// // sio_test_sample.cpp // // Created by Melo Yao on 3/24/15. // #include "../../src/sio_client.h" #include #include #include #include #include #include #ifdef WIN32 #define HIGHLIGHT(__O__) std::cout<<__O__< #include #define MAIN_FUNC int _tmain(int argc, _TCHAR* argv[]) #else #define HIGHLIGHT(__O__) std::cout<<"\e[1;31m"<<__O__<<"\e[0m"<on("new message", sio::socket::event_listener_aux([&](string const& name, message::ptr const& data, bool isAck,message::list &ack_resp) { _lock.lock(); string user = data->get_map()["username"]->get_string(); string message = data->get_map()["message"]->get_string(); EM(user<<":"<on("user joined",sio::socket::event_listener_aux([&](string const& name, message::ptr const& data, bool isAck,message::list &ack_resp) { _lock.lock(); string user = data->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(); })); current_socket->on("user left", sio::socket::event_listener_aux([&](string const& name, message::ptr const& data, bool isAck,message::list &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(); })); } MAIN_FUNC { sio::client h; connection_listener l(h); h.set_open_listener(std::bind(&connection_listener::on_connected, &l)); h.set_close_listener(std::bind(&connection_listener::on_close, &l,std::placeholders::_1)); h.set_fail_listener(std::bind(&connection_listener::on_fail, &l)); h.connect("http://127.0.0.1:3000"); _lock.lock(); if(!connect_finish) { _cond.wait(_lock); } _lock.unlock(); current_socket = h.socket(); Login: string nickname; while (nickname.length() == 0) { HIGHLIGHT("Type your nickname:"); getline(cin, nickname); } current_socket->on("login", sio::socket::event_listener_aux([&](string const& name, message::ptr const& data, bool isAck,message::list &ack_resp){ _lock.lock(); participants = data->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(); current_socket->off("login"); })); current_socket->emit("add user", nickname); _lock.lock(); if (participants<0) { _cond.wait(_lock); } _lock.unlock(); bind_events(); HIGHLIGHT("Start to chat,commands:\n'$exit' : exit chat\n'$nsp ' : change namespace"); for (std::string line; std::getline(std::cin, line);) { if(line.length()>0) { if(line == "$exit") { break; } else if(line.length() > 5&&line.substr(0,5) == "$nsp ") { string new_nsp = line.substr(5); if(new_nsp == current_socket->get_namespace()) { continue; } current_socket->off_all(); current_socket->off_error(); //per socket.io, default nsp should never been closed. if(current_socket->get_namespace() != "/") { current_socket->close(); } current_socket = h.socket(new_nsp); bind_events(); //if change to default nsp, we do not need to login again (since it is not closed). if(current_socket->get_namespace() == "/") { continue; } goto Login; } current_socket->emit("new message", line); _lock.lock(); EM("\t\t\t"<