Exemple #1
0
int main(int argc, char** argv) {

    IOLoop loop;

    SharedClient client = make_shared<echolib::Client>(loop, string(argv[1]));

    if (argc > 2) {

        bool write = false;

        string data = slurp(string(argv[2]));

        function<void(int)> subscribe_callback = [&](int m) {
            write = m > 0;
        };

        TypedPublisher<string,true> pub(client, "chunked");
        SubscriptionWatcher watch(client, "chunked", subscribe_callback);

        int counter = 0;

        while(loop.wait(10)) {
            counter++;
            if (write && counter == 50) {
                std::cout << "Writing data" << std::endl;
                pub.send(data);
            }
            counter = counter % 50;
        }

    } else {
        function<void(shared_ptr<string>)> chunked_callback = [&](shared_ptr<string> m) {

            std::cout << "Received " << m->size() << " bytes" << std::endl;

        };

        TypedSubscriber<string, true> sub(client, "chunked", chunked_callback);

        while(loop.wait(10)) {
        }

    }

    exit(0);
}
Exemple #2
0
int main(int argc, char** argv) {

    IOLoop loop;

    //Connect to local socket based daemon
    SharedClient client = make_shared<echolib::Client>(loop, string(argv[1]));

    string name;
    cout << "Enter your name\n";
    std::getline(std::cin, name);

    std::mutex mutex;

    function<void(shared_ptr<pair<string, string> >)> chat_callback = [&](shared_ptr<pair<string, string> > m) {
        const string name = m->first;
        const string message = m->second;
        std::cout<< name << ": " << message << std::endl;
    };

    function<void(int)> subscribe_callback = [&](int m) {
        std::cout << "Total subscribers: " << m << std::endl;
    };

    TypedSubscriber<pair<string, string> > sub(client, "chat", chat_callback);
    SubscriptionWatcher watch(client, "chat", subscribe_callback);
    TypedPublisher<pair<string, string> > pub(client, "chat");

    std::thread write([&]() {
        while (client->is_connected()) {
            string message;
            std::getline(std::cin, message);
            pub.send(pair<string, string>(name, message));
        }

    });

    while(loop.wait(10)) {
        // We have to give the write thread some space
        std::this_thread::sleep_for(std::chrono::milliseconds(10));
    }

    exit(0);
}