Skip to content

Commit

Permalink
fixes, clean, and std::string for ping/pong
Browse files Browse the repository at this point in the history
  • Loading branch information
Alexandre Konieczny authored and Alexandre Konieczny committed Jun 6, 2019
1 parent 65a3d36 commit ce53a15
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 17 deletions.
14 changes: 1 addition & 13 deletions Release/include/cpprest/ws_client.h
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,7 @@ class websocket_client_config
/// <summary>
/// Creates a websocket client configuration with default settings.
/// </summary>
websocket_client_config() : m_sni_enabled(true), m_validate_certificates(true), m_auto_pong_enabled(false) {}
websocket_client_config() : m_sni_enabled(true), m_validate_certificates(true) {}

/// <summary>
/// Get the web proxy object
Expand All @@ -105,17 +105,6 @@ class websocket_client_config
/// <param name="cred">The client credentials.</param>
void set_credentials(const web::credentials& cred) { m_credentials = cred; }

/// <summary>
/// Turn on automatic pong answer to incoming ping request
/// </summary>
void enable_auto_pong() { m_auto_pong_enabled = true; }

/// <summary>
/// Determines if the automatic Pong answer to Ping is enabled.
/// </summary>
/// <returns>True if enabled, false otherwise.</returns>
bool is_auto_pong_enabled() const { return m_auto_pong_enabled; }

/// <summary>
/// Disables Server Name Indication (SNI). Default is on.
/// </summary>
Expand Down Expand Up @@ -217,7 +206,6 @@ class websocket_client_config
bool m_sni_enabled;
utf8string m_sni_hostname;
bool m_validate_certificates;
bool m_auto_pong_enabled;
#if !defined(_WIN32) || !defined(__cplusplus_winrt)
std::function<void(boost::asio::ssl::context&)> m_ssl_context_callback;
#endif
Expand Down
2 changes: 1 addition & 1 deletion Release/include/cpprest/ws_msg.h
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,7 @@ class websocket_outgoing_message
#if !defined(__cplusplus_winrt)
/// <summary>
/// Sets the outgoing message to be a ping message.
/// This is useful when the client side wants to check whether the server is alive.
/// </summary>
/// <param name="data">UTF-8 String containing the optional ping message.</param>
void set_ping_message(const std::string& data = "")
Expand All @@ -70,7 +71,6 @@ class websocket_outgoing_message

/// <summary>
/// Sets 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>
/// <param name="data">UTF-8 String containing the optional pong message.</param>
void set_pong_message(const std::string& data = "")
Expand Down
16 changes: 13 additions & 3 deletions Release/src/websockets/client/ws_client_wspp.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -337,7 +337,7 @@ class wspp_callback_client : public websocket_client_callback_impl,

m_external_message_handler(incoming_msg);
}
return m_config.is_auto_pong_enabled();
return true;
});

client.set_pong_handler(
Expand Down Expand Up @@ -724,8 +724,18 @@ class wspp_callback_client : public websocket_client_callback_impl,
case websocket_message_type::binary_message:
client.send(this_client->m_con, sp_allocated.get(), length, websocketpp::frame::opcode::binary, ec);
break;
case websocket_message_type::ping: client.ping(this_client->m_con, *(std::string*)sp_allocated.get(), ec); break;
case websocket_message_type::pong: client.pong(this_client->m_con, *(std::string*)sp_allocated.get(), ec); break;
case websocket_message_type::ping:
{
std::string s(reinterpret_cast<char*>(sp_allocated.get()), length);
client.ping(this_client->m_con, s, ec);
break;
}
case websocket_message_type::pong:
{
std::string s(reinterpret_cast<char*>(sp_allocated.get()), length);
client.pong(this_client->m_con, s, ec);
break;
}
default:
// This case should have already been filtered above.
std::abort();
Expand Down

0 comments on commit ce53a15

Please sign in to comment.