diff --git a/src/internal/sio_client_impl.cpp b/src/internal/sio_client_impl.cpp index fa948f4c..52540208 100644 --- a/src/internal/sio_client_impl.cpp +++ b/src/internal/sio_client_impl.cpp @@ -303,37 +303,14 @@ namespace sio } } - void client_impl::ping(const asio::error_code& ec) - { - if(ec || m_con.expired()) - { - if (ec != asio::error::operation_aborted) - LOG("ping exit,con is expired?"< payload) - { - lib::error_code ec; - this->m_client.send(this->m_con, *payload, frame::opcode::text, ec); - }); - if(!m_ping_timeout_timer) - { - m_ping_timeout_timer.reset(new asio::steady_timer(m_client.get_io_service())); - std::error_code timeout_ec; - m_ping_timeout_timer->expires_from_now(milliseconds(m_ping_timeout), timeout_ec); - m_ping_timeout_timer->async_wait(std::bind(&client_impl::timeout_pong, this, std::placeholders::_1)); - } - } - - void client_impl::timeout_pong(const asio::error_code &ec) + void client_impl::timeout_ping(const asio::error_code &ec) { if(ec) { return; } - LOG("Pong timeout"<expires_from_now(milliseconds(m_ping_timeout),ec); - m_ping_timeout_timer->async_wait(std::bind(&client_impl::timeout_pong, this, std::placeholders::_1)); - } // Parse the incoming message according to socket.IO rules m_packet_mgr.put_payload(msg->get_payload()); } @@ -525,6 +497,9 @@ namespace sio m_ping_timeout = 60000; } + // Start ping timeout + update_ping_timeout_timer(); + return; } failed: @@ -534,17 +509,15 @@ namespace sio void client_impl::on_ping() { + // Reply with pong packet. packet p(packet::frame_pong); m_packet_mgr.encode(p, [&](bool /*isBin*/,shared_ptr payload) { this->m_client.send(this->m_con, *payload, frame::opcode::text); }); - if(m_ping_timeout_timer) - { - m_ping_timeout_timer->cancel(); - m_ping_timeout_timer.reset(); - } + // Reset the ping timeout. + update_ping_timeout_timer(); } void client_impl::on_decode(packet const& p) @@ -589,6 +562,16 @@ namespace sio m_ping_timeout_timer.reset(); } } + + void client_impl::update_ping_timeout_timer() { + if (!m_ping_timeout_timer) { + m_ping_timeout_timer = std::unique_ptr(new asio::steady_timer(get_io_service())); + } + + asio::error_code ec; + m_ping_timeout_timer->expires_from_now(milliseconds(m_ping_interval + m_ping_timeout), ec); + m_ping_timeout_timer->async_wait(std::bind(&client_impl::timeout_ping, this, std::placeholders::_1)); + } void client_impl::reset_states() { diff --git a/src/internal/sio_client_impl.h b/src/internal/sio_client_impl.h index 856622ab..bbc5ae6a 100644 --- a/src/internal/sio_client_impl.h +++ b/src/internal/sio_client_impl.h @@ -151,7 +151,7 @@ namespace sio void ping(const asio::error_code& ec); - void timeout_pong(const asio::error_code& ec); + void timeout_ping(const asio::error_code& ec); void timeout_reconnect(asio::error_code const& ec); @@ -181,6 +181,8 @@ namespace sio void reset_states(); void clear_timers(); + + void update_ping_timeout_timer(); #if SIO_TLS typedef websocketpp::lib::shared_ptr context_ptr;