Skip to content

Commit 4d65010

Browse files
committed
interface change, move 'socket#connect(open)/close listener' to client
1 parent e89baf4 commit 4d65010

7 files changed

Lines changed: 158 additions & 81 deletions

File tree

examples/Console/main.cpp

Lines changed: 67 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -68,12 +68,48 @@ class connection_listener
6868

6969
int 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"<<"\nthere"<<(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"<<"\nthere"<<(plural?" are ":"'s ")<< participants<<(plural?" participants":" participant"));
102+
_lock.unlock();
103+
});
104+
}
105+
71106
MAIN_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-\nthere"<<(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"<<"\nthere"<<(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"<<"\nthere"<<(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();

src/internal/sio_client_impl.cpp

Lines changed: 27 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -303,6 +303,16 @@ namespace sio {
303303
return m_client.get_io_service();
304304
}
305305

306+
void client_impl::on_socket_closed(std::string const& nsp)
307+
{
308+
if(m_socket_close_listener)m_socket_close_listener(nsp);
309+
}
310+
311+
void client_impl::on_socket_opened(std::string const& nsp)
312+
{
313+
if(m_socket_open_listener)m_socket_open_listener(nsp);
314+
}
315+
306316
void client_impl::__send(std::shared_ptr<const std::string> const& payload_ptr,frame::opcode::value opcode)
307317
{
308318
if(m_con_state == con_opened)
@@ -314,7 +324,12 @@ namespace sio {
314324
m_ping_timer->expires_from_now(milliseconds(m_ping_interval),timeout_ec);
315325
m_ping_timer->async_wait(lib::bind(&client_impl::__ping,this,lib::placeholders::_1));
316326
}
317-
m_client.send(m_con,*payload_ptr,opcode);
327+
lib::error_code ec;
328+
m_client.send(m_con,*payload_ptr,opcode,ec);
329+
if(ec)
330+
{
331+
std::cerr<<"Send failed,reason:"<< ec.message()<<std::endl;
332+
}
318333
}
319334
}
320335

@@ -420,11 +435,20 @@ namespace sio {
420435
socket::ptr const& client_impl::socket(std::string const& nsp)
421436
{
422437
std::lock_guard<std::mutex> guard(m_socket_mutex);
423-
std::string aux = nsp;
424-
if(aux == "")
438+
std::string aux;
439+
if(nsp == "")
425440
{
426441
aux = "/";
427442
}
443+
else if( nsp[0] != '/')
444+
{
445+
aux.append("/",1);
446+
aux.append(nsp);
447+
}
448+
else
449+
{
450+
aux = nsp;
451+
}
428452

429453
auto it = m_sockets.find(aux);
430454
if(it!= m_sockets.end())

src/internal/sio_client_impl.h

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,10 @@ void set_##__FIELD__(__TYPE__ const& l) \
5959

6060
SYNTHESIS_SETTER(client::close_listener,close_listener)
6161

62+
SYNTHESIS_SETTER(client::socket_listener,socket_open_listener)
63+
64+
SYNTHESIS_SETTER(client::socket_listener,socket_close_listener)
65+
6266
#undef SYNTHESIS_SETTER
6367

6468

@@ -69,6 +73,12 @@ void set_##__FIELD__(__TYPE__ const& l) \
6973
m_fail_listener = nullptr;
7074
}
7175

76+
void clear_socket_listeners()
77+
{
78+
m_socket_open_listener = nullptr;
79+
m_socket_close_listener = nullptr;
80+
}
81+
7282
// Client Functions - such as send, etc.
7383
void connect(const std::string& uri);
7484

@@ -93,6 +103,10 @@ void set_##__FIELD__(__TYPE__ const& l) \
93103

94104
boost::asio::io_service& get_io_service();
95105

106+
void on_socket_closed(std::string const& nsp);
107+
108+
void on_socket_opened(std::string const& nsp);
109+
96110
private:
97111
void __close(close::status::value const& code,std::string const& reason);
98112

@@ -152,6 +166,9 @@ void set_##__FIELD__(__TYPE__ const& l) \
152166
client::con_listener m_fail_listener;
153167
client::close_listener m_close_listener;
154168

169+
client::socket_listener m_socket_open_listener;
170+
client::socket_listener m_socket_close_listener;
171+
155172
std::map<const std::string,socket::ptr> m_sockets;
156173

157174
std::mutex m_socket_mutex;

src/sio_client.cpp

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,11 +39,27 @@ namespace sio
3939
m_impl->set_close_listener(l);
4040
}
4141

42+
43+
void client::set_socket_open_listener(socket_listener const& l)
44+
{
45+
m_impl->set_socket_open_listener(l);
46+
}
47+
48+
void client::set_socket_close_listener(socket_listener const& l)
49+
{
50+
m_impl->set_socket_close_listener(l);
51+
}
52+
4253
void client::clear_con_listeners()
4354
{
4455
m_impl->clear_con_listeners();
4556
}
4657

58+
void client::clear_socket_listeners()
59+
{
60+
m_impl->clear_socket_listeners();
61+
}
62+
4763
void client::connect(const std::string& uri)
4864
{
4965
m_impl->connect(uri);

src/sio_client.h

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,8 @@ namespace sio {
2727

2828
typedef std::function<void(close_reason const& reason)> close_listener;
2929

30+
typedef std::function<void(std::string const& nsp)> socket_listener;
31+
3032
client();
3133
~client();
3234

@@ -37,8 +39,14 @@ namespace sio {
3739

3840
void set_close_listener(close_listener const& l);
3941

42+
void set_socket_open_listener(socket_listener const& l);
43+
44+
void set_socket_close_listener(socket_listener const& l);
45+
4046
void clear_con_listeners();
4147

48+
void clear_socket_listeners();
49+
4250
// Client Functions - such as send, etc.
4351
void connect(const std::string& uri);
4452

0 commit comments

Comments
 (0)