// // sio_test_sample.cpp // // Created by Melo Yao on 3/24/15. // #include "../../src/sio_client.h" #include #include #include #include #include #include #define HIGHLIGHT(__O__) std::cout<<"\e[1;31m"<<__O__<<"\e[0m"<get_map()["numUsers"]->get_int(); bool plural = participants !=1; HIGHLIGHT("Welcome to Socket.IO Chat-\nthere"<<(plural?" are ":"'s ")<< participants<<(plural?" participants":"participant")); _cond.notify_all(); _lock.unlock(); h.unbind_event("login"); }); h.emit("add user", nickname); _lock.lock(); if (participants<0) { _cond.wait(_lock); } _lock.unlock(); h.bind_event("new message", [&](string const& name, message::ptr const& data, bool isAck,message::ptr &ack_resp) { _lock.lock(); string user = data->get_map()["username"]->get_string(); string message = data->get_map()["message"]->get_string(); EM(user<<":"<get_map()["username"]->get_string(); participants = data->get_map()["numUsers"]->get_int(); bool plural = participants !=1; // abc " HIGHLIGHT(user<<" joined"<<"\nthere"<<(plural?" are ":"'s ")<< participants<<(plural?" participants":"participant")); _lock.unlock(); }); h.bind_event("user left", [&](string const& name, message::ptr const& data, bool isAck,message::ptr &ack_resp) { _lock.lock(); string user = data->get_map()["username"]->get_string(); participants = data->get_map()["numUsers"]->get_int(); bool plural = participants !=1; HIGHLIGHT(user<<" left"<<"\nthere"<<(plural?" are ":"'s ")<< participants<<(plural?" participants":"participant")); _lock.unlock(); }); HIGHLIGHT("Start to chat, type '$exit' to exit"); for (std::string line; std::getline(std::cin, line);) { if(line.length()>0) { if(line == "$exit") { break; } h.emit("new message", line); _lock.lock(); EM("\t\t\t"<