Skip to content

Commit 5a54812

Browse files
committed
Add method to send a websocket ping
1 parent 1598331 commit 5a54812

File tree

2 files changed

+11
-1
lines changed

2 files changed

+11
-1
lines changed

easywsclient.cpp

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -112,6 +112,7 @@ class _DummyWebSocket : public easywsclient::WebSocket
112112
public:
113113
void poll(int timeout) { }
114114
void send(const std::string& message) { }
115+
void sendPing() { }
115116
void close() { }
116117
void _dispatch(Callback & callable) { }
117118
readyStateValues getReadyState() const { return CLOSED; }
@@ -305,7 +306,15 @@ class _RealWebSocket : public easywsclient::WebSocket
305306
}
306307
}
307308

309+
void sendPing() {
310+
sendData(wsheader_type::PING, std::string());
311+
}
312+
308313
void send(const std::string& message) {
314+
sendData(wsheader_type::TEXT_FRAME, message);
315+
}
316+
317+
void sendData(wsheader_type::opcode_type type, const std::string& message) {
309318
// TODO:
310319
// Masking key should (must) be derived from a high quality random
311320
// number generator, to mitigate attacks on non-WebSocket friendly
@@ -316,7 +325,7 @@ class _RealWebSocket : public easywsclient::WebSocket
316325
std::vector<uint8_t> header;
317326
uint64_t message_size = message.size();
318327
header.assign(2 + (message_size >= 126 ? 2 : 0) + (message_size >= 65536 ? 6 : 0) + (useMask ? 4 : 0), 0);
319-
header[0] = 0x80 | wsheader_type::TEXT_FRAME;
328+
header[0] = 0x80 | type;
320329
if (false) { }
321330
else if (message_size < 126) {
322331
header[1] = (message_size & 0xff) | (useMask ? 0x80 : 0);

easywsclient.hpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ class WebSocket {
2626
virtual ~WebSocket() { }
2727
virtual void poll(int timeout = 0) = 0; // timeout in milliseconds
2828
virtual void send(const std::string& message) = 0;
29+
virtual void sendPing() = 0;
2930
virtual void close() = 0;
3031
virtual readyStateValues getReadyState() const = 0;
3132
template<class Callable>

0 commit comments

Comments
 (0)