Skip to content

Commit e89baf4

Browse files
committed
change some socket behavior, make more reasonable
1 parent 958cfad commit e89baf4

4 files changed

Lines changed: 38 additions & 7 deletions

File tree

examples/iOS/SioChatDemo/SioChatDemo/CRViewController.mm

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -181,7 +181,8 @@ - (void)viewDidLoad
181181

182182
-(void)viewWillAppear:(BOOL)animated
183183
{
184-
184+
_io->set_close_listener(std::bind(&OnClose, (__bridge CFTypeRef)self, std::placeholders::_1));
185+
_io->set_fail_listener(std::bind(&OnFailed, (__bridge CFTypeRef)self));
185186
}
186187

187188
-(void)keyboardWillShow:(NSNotification*)notification
@@ -401,7 +402,6 @@ -(BOOL)textFieldShouldReturn:(UITextField *)textField
401402
using std::placeholders::_4;
402403
socket::ptr socket = _io->socket();
403404
socket->set_connect_listener(std::bind(&OnConnected, (__bridge CFTypeRef)self));
404-
socket->set_close_listener(std::bind(&OnFailed, (__bridge CFTypeRef)self));
405405

406406
socket->on("new message", std::bind(&OnNewMessage, (__bridge CFTypeRef)self, _1,_2,_3,_4));
407407
socket->on("typing", std::bind(&OnTyping, (__bridge CFTypeRef)self, _1,_2,_3,_4));

src/internal/sio_client_impl.cpp

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,7 @@ namespace sio {
5050

5151
client_impl::~client_impl()
5252
{
53+
this->sockets_invoke_void(&sio::socket::on_close);
5354
sync_close();
5455
}
5556

@@ -58,12 +59,11 @@ namespace sio {
5859
{
5960
m_con.reset();
6061
m_con_state = con_closed;
61-
this->sockets_invoke_void(&sio::socket::on_close);
62+
this->sockets_invoke_void(&sio::socket::on_disconnect);
6263
LOG("Connection failed." << std::endl);
6364
if(m_fail_listener)m_fail_listener();
6465
}
6566

66-
6767
void client_impl::on_pong()
6868
{
6969
if(m_ping_timeout_timer)
@@ -147,12 +147,12 @@ namespace sio {
147147
client::close_reason reason;
148148
if(code == close::status::normal)
149149
{
150-
this->sockets_invoke_void(&sio::socket::on_close);
150+
this->sockets_invoke_void(&sio::socket::on_disconnect);
151151
reason = client::close_reason_normal;
152152
}
153153
else
154154
{
155-
this->sockets_invoke_void(&sio::socket::on_close);
155+
this->sockets_invoke_void(&sio::socket::on_disconnect);
156156
reason = client::close_reason_drop;
157157
}
158158

src/sio_socket.cpp

Lines changed: 29 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -124,6 +124,7 @@ void set_##__FIELD__(__TYPE__ const& l) \
124124
SYNTHESIS_SETTER(error_listener, error_listener) //socket io errors
125125

126126
SYNTHESIS_SETTER(con_listener, close_listener) //socket io errors
127+
127128
#undef SYNTHESIS_SETTER
128129

129130
void clear_listeners()
@@ -158,6 +159,8 @@ void set_##__FIELD__(__TYPE__ const& l) \
158159

159160
void on_message_packet(packet const& packet);
160161

162+
void on_disconnect();
163+
161164
private:
162165

163166
// Message Parsing callbacks.
@@ -282,6 +285,14 @@ void set_##__FIELD__(__TYPE__ const& l) \
282285
{
283286
packet p(packet::type_disconnect,m_nsp);
284287
__send_packet(p);
288+
289+
if(!m_connection_timer)
290+
{
291+
m_connection_timer.reset(new boost::asio::deadline_timer(m_client->get_io_service()));
292+
}
293+
boost::system::error_code ec;
294+
m_connection_timer->expires_from_now(boost::posix_time::milliseconds(3000), ec);
295+
m_connection_timer->async_wait(lib::bind(&socket::impl::on_close, this));
285296
}
286297
}
287298

@@ -331,6 +342,18 @@ void set_##__FIELD__(__TYPE__ const& l) \
331342
__send_connect();
332343
}
333344

345+
void socket::impl::on_disconnect()
346+
{
347+
NULL_GUARD(m_client);
348+
if(m_connected)
349+
{
350+
m_connected = false;
351+
while (!m_packet_queue.empty()) {
352+
m_packet_queue.pop();
353+
}
354+
}
355+
}
356+
334357
void socket::impl::on_message_packet(packet const& p)
335358
{
336359
NULL_GUARD(m_client);
@@ -455,7 +478,7 @@ void set_##__FIELD__(__TYPE__ const& l) \
455478
}
456479
m_connection_timer.reset();
457480
LOG("Connection timeout"<<std::endl);
458-
this->on_close();
481+
this->on_disconnect();
459482
}
460483

461484
void socket::impl::__send_packet(sio::packet &p)
@@ -583,6 +606,11 @@ void set_##__FIELD__(__TYPE__ const& l) \
583606
{
584607
m_impl->on_message_packet(p);
585608
}
609+
610+
void socket::on_disconnect()
611+
{
612+
m_impl->on_disconnect();
613+
}
586614
}
587615

588616

src/sio_socket.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,7 @@ namespace sio
6565
void close();
6666

6767
void set_connect_listener(con_listener const& l);
68+
6869
void set_close_listener(con_listener const& l);
6970

7071
void set_error_listener(error_listener const& l);
@@ -94,6 +95,8 @@ namespace sio
9495

9596
void on_open();
9697

98+
void on_disconnect();
99+
97100
void on_message_packet(packet const& p);
98101

99102
friend class client_impl;

0 commit comments

Comments
 (0)