From 5a54812efed89480a6e6911f365d3326ab9373bd Mon Sep 17 00:00:00 2001 From: Donald Pillou Date: Wed, 1 Jan 2014 20:21:11 +0100 Subject: [PATCH] Add method to send a websocket ping --- easywsclient.cpp | 11 ++++++++++- easywsclient.hpp | 1 + 2 files changed, 11 insertions(+), 1 deletion(-) diff --git a/easywsclient.cpp b/easywsclient.cpp index 7fac4ef..1957db4 100644 --- a/easywsclient.cpp +++ b/easywsclient.cpp @@ -112,6 +112,7 @@ class _DummyWebSocket : public easywsclient::WebSocket public: void poll(int timeout) { } void send(const std::string& message) { } + void sendPing() { } void close() { } void _dispatch(Callback & callable) { } readyStateValues getReadyState() const { return CLOSED; } @@ -305,7 +306,15 @@ class _RealWebSocket : public easywsclient::WebSocket } } + void sendPing() { + sendData(wsheader_type::PING, std::string()); + } + void send(const std::string& message) { + sendData(wsheader_type::TEXT_FRAME, message); + } + + void sendData(wsheader_type::opcode_type type, const std::string& message) { // TODO: // Masking key should (must) be derived from a high quality random // number generator, to mitigate attacks on non-WebSocket friendly @@ -316,7 +325,7 @@ class _RealWebSocket : public easywsclient::WebSocket std::vector header; uint64_t message_size = message.size(); header.assign(2 + (message_size >= 126 ? 2 : 0) + (message_size >= 65536 ? 6 : 0) + (useMask ? 4 : 0), 0); - header[0] = 0x80 | wsheader_type::TEXT_FRAME; + header[0] = 0x80 | type; if (false) { } else if (message_size < 126) { header[1] = (message_size & 0xff) | (useMask ? 0x80 : 0); diff --git a/easywsclient.hpp b/easywsclient.hpp index 0daabdb..3607b66 100644 --- a/easywsclient.hpp +++ b/easywsclient.hpp @@ -26,6 +26,7 @@ class WebSocket { virtual ~WebSocket() { } virtual void poll(int timeout = 0) = 0; // timeout in milliseconds virtual void send(const std::string& message) = 0; + virtual void sendPing() = 0; virtual void close() = 0; virtual readyStateValues getReadyState() const = 0; template