diff --git a/Release/include/cpprest/ws_msg.h b/Release/include/cpprest/ws_msg.h index 9b3fe713a7..171234edf0 100644 --- a/Release/include/cpprest/ws_msg.h +++ b/Release/include/cpprest/ws_msg.h @@ -73,6 +73,15 @@ class websocket_outgoing_message { public: + /// + /// Sets a the outgoing message to be an unsolicited pong message. + /// This is useful when the client side wants to check whether the server is alive. + /// + void set_pong_message() + { + this->set_message_pong(); + } + /// /// Sets a UTF-8 message as the message body. /// @@ -152,6 +161,14 @@ class websocket_outgoing_message const pplx::task_completion_event & body_sent() const { return m_body_sent; } + void set_message_pong() + { + concurrency::streams::container_buffer buffer(""); + m_msg_type = websocket_message_type::pong; + m_length = static_cast(buffer.size()); + m_body = buffer; + } + void set_message(const concurrency::streams::container_buffer &buffer) { m_msg_type = websocket_message_type::text_message; diff --git a/Release/src/websockets/client/ws_client_wspp.cpp b/Release/src/websockets/client/ws_client_wspp.cpp index 1e53f69e39..bd17af922e 100644 --- a/Release/src/websockets/client/ws_client_wspp.cpp +++ b/Release/src/websockets/client/ws_client_wspp.cpp @@ -373,13 +373,14 @@ class wspp_callback_client : public websocket_client_callback_impl, public std:: { case websocket_message_type::text_message: case websocket_message_type::binary_message: + case websocket_message_type::pong: break; default: return pplx::task_from_exception(websocket_exception("Invalid message type")); } const auto length = msg.m_length; - if (length == 0) + if (length == 0 && msg.m_msg_type != websocket_message_type::pong) { return pplx::task_from_exception(websocket_exception("Cannot send empty message.")); } @@ -639,13 +640,19 @@ class wspp_callback_client : public websocket_client_callback_impl, public std:: ec); break; case websocket_message_type::binary_message: - client.send( + client.send( this_client->m_con, sp_allocated.get(), length, websocketpp::frame::opcode::binary, ec); break; + case websocket_message_type::pong: + client.pong( + this_client->m_con, + "", + ec); + break; default: // This case should have already been filtered above. std::abort();