Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add unsolicited pong support #15

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
17 changes: 17 additions & 0 deletions Release/include/cpprest/ws_msg.h
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,15 @@ class websocket_outgoing_message
{
public:

/// <summary>
/// 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.
/// </summary>
void set_pong_message()
{
this->set_message_pong();
}

/// <summary>
/// Sets a UTF-8 message as the message body.
/// </summary>
Expand Down Expand Up @@ -152,6 +161,14 @@ class websocket_outgoing_message

const pplx::task_completion_event<void> & body_sent() const { return m_body_sent; }

void set_message_pong()
{
concurrency::streams::container_buffer<std::string> buffer("");
m_msg_type = websocket_message_type::pong;
m_length = static_cast<size_t>(buffer.size());
m_body = buffer;
}

void set_message(const concurrency::streams::container_buffer<std::string> &buffer)
{
m_msg_type = websocket_message_type::text_message;
Expand Down
11 changes: 9 additions & 2 deletions Release/src/websockets/client/ws_client_wspp.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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<void>(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<void>(websocket_exception("Cannot send empty message."));
}
Expand Down Expand Up @@ -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();
Expand Down