@@ -68,12 +68,48 @@ class connection_listener
6868
6969int participants = -1 ;
7070
71+ socket::ptr current_socket;
72+
73+ void bind_events (socket::ptr &socket)
74+ {
75+ current_socket->on (" new message" , [&](string const & name, message::ptr const & data, bool isAck,message::ptr &ack_resp)
76+ {
77+ _lock.lock ();
78+ string user = data->get_map ()[" username" ]->get_string ();
79+ string message = data->get_map ()[" message" ]->get_string ();
80+ EM (user<<" :" <<message);
81+ _lock.unlock ();
82+ });
83+
84+ current_socket->on (" user joined" , [&](string const & name, message::ptr const & data, bool isAck,message::ptr &ack_resp)
85+ {
86+ _lock.lock ();
87+ string user = data->get_map ()[" username" ]->get_string ();
88+ participants = data->get_map ()[" numUsers" ]->get_int ();
89+ bool plural = participants !=1 ;
90+
91+ // abc "
92+ HIGHLIGHT (user<<" joined" <<" \n there" <<(plural?" are " :" 's " )<< participants<<(plural?" participants" :" participant" ));
93+ _lock.unlock ();
94+ });
95+ current_socket->on (" user left" , [&](string const & name, message::ptr const & data, bool isAck,message::ptr &ack_resp)
96+ {
97+ _lock.lock ();
98+ string user = data->get_map ()[" username" ]->get_string ();
99+ participants = data->get_map ()[" numUsers" ]->get_int ();
100+ bool plural = participants !=1 ;
101+ HIGHLIGHT (user<<" left" <<" \n there" <<(plural?" are " :" 's " )<< participants<<(plural?" participants" :" participant" ));
102+ _lock.unlock ();
103+ });
104+ }
105+
71106MAIN_FUNC
72107{
73108
74109 sio::client h;
75110 connection_listener l (h);
76- h.set_connect_listener (std::bind (&connection_listener::on_connected, &l));
111+ current_socket = h.socket ();
112+ h.set_open_listener (std::bind (&connection_listener::on_connected, &l));
77113 h.set_close_listener (std::bind (&connection_listener::on_close, &l,std::placeholders::_1));
78114 h.set_fail_listener (std::bind (&connection_listener::on_fail, &l));
79115 h.connect (" http://127.0.0.1:3000" );
@@ -83,66 +119,62 @@ MAIN_FUNC
83119 _cond.wait (_lock);
84120 }
85121 _lock.unlock ();
122+ Login:
86123 string nickname;
87124 while (nickname.length () == 0 ) {
88125 HIGHLIGHT (" Type your nickname:" );
89126
90127 getline (cin, nickname);
91128 }
92- h. bind_event (" login" , [&](string const & name, message::ptr const & data, bool isAck,message::ptr &ack_resp){
129+ current_socket-> on (" login" , [&](string const & name, message::ptr const & data, bool isAck,message::ptr &ack_resp){
93130 _lock.lock ();
94131 participants = data->get_map ()[" numUsers" ]->get_int ();
95132 bool plural = participants !=1 ;
96133 HIGHLIGHT (" Welcome to Socket.IO Chat-\n there" <<(plural?" are " :" 's " )<< participants<<(plural?" participants" :" participant" ));
97134 _cond.notify_all ();
98135 _lock.unlock ();
99- h. unbind_event (" login" );
136+ current_socket-> off (" login" );
100137 });
101- h. emit (" add user" , nickname);
138+ current_socket-> emit (" add user" , nickname);
102139 _lock.lock ();
103140 if (participants<0 ) {
104141 _cond.wait (_lock);
105142 }
106143 _lock.unlock ();
107-
108- h.bind_event (" new message" , [&](string const & name, message::ptr const & data, bool isAck,message::ptr &ack_resp)
109- {
110- _lock.lock ();
111- string user = data->get_map ()[" username" ]->get_string ();
112- string message = data->get_map ()[" message" ]->get_string ();
113- EM (user<<" :" <<message);
114- _lock.unlock ();
115- });
144+ bind_events (current_socket);
116145
117- h.bind_event (" user joined" , [&](string const & name, message::ptr const & data, bool isAck,message::ptr &ack_resp)
118- {
119- _lock.lock ();
120- string user = data->get_map ()[" username" ]->get_string ();
121- participants = data->get_map ()[" numUsers" ]->get_int ();
122- bool plural = participants !=1 ;
123-
124- // abc "
125- HIGHLIGHT (user<<" joined" <<" \n there" <<(plural?" are " :" 's " )<< participants<<(plural?" participants" :" participant" ));
126- _lock.unlock ();
127- });
128- h.bind_event (" user left" , [&](string const & name, message::ptr const & data, bool isAck,message::ptr &ack_resp)
129- {
130- _lock.lock ();
131- string user = data->get_map ()[" username" ]->get_string ();
132- participants = data->get_map ()[" numUsers" ]->get_int ();
133- bool plural = participants !=1 ;
134- HIGHLIGHT (user<<" left" <<" \n there" <<(plural?" are " :" 's " )<< participants<<(plural?" participants" :" participant" ));
135- _lock.unlock ();
136- });
137- HIGHLIGHT (" Start to chat, type '$exit' to exit" );
146+ HIGHLIGHT (" Start to chat,commands:\n '$exit' : exit chat\n '$nsp <namespace>' : change namespace" );
138147 for (std::string line; std::getline (std::cin, line);) {
139148 if (line.length ()>0 )
140149 {
141150 if (line == " $exit" )
142151 {
143152 break ;
144153 }
145- h.emit (" new message" , line);
154+ else if (line.length () > 5 &&line.substr (0 ,5 ) == " $nsp " )
155+ {
156+ string new_nsp = line.substr (5 );
157+ if (new_nsp == current_socket->get_namespace ())
158+ {
159+ continue ;
160+ }
161+ current_socket->off_all ();
162+ current_socket->off_error ();
163+ // per socket.io, default nsp should never been closed.
164+ if (current_socket->get_namespace () != " /" )
165+ {
166+ current_socket->close ();
167+ }
168+ current_socket = h.socket (new_nsp);
169+ // if change to default nsp, we do not need to bind events again.
170+ if (current_socket->get_namespace () == " /" )
171+ {
172+ continue ;
173+ }
174+ bind_events (current_socket);
175+ goto Login;
176+ }
177+ current_socket->emit (" new message" , line);
146178 _lock.lock ();
147179 EM (" \t\t\t " <<line<<" :" <<" You" );
148180 _lock.unlock ();
0 commit comments