Add method to send a websocket ping

This commit is contained in:
Donald Pillou
2014-01-01 20:21:11 +01:00
parent 1598331652
commit 5a54812efe
2 changed files with 11 additions and 1 deletions
+10 -1
View File
@@ -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<uint8_t> 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);
+1
View File
@@ -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<class Callable>