Merge pull request #26 from donpillou/sendPing

Add method to send a websocket ping
This commit is contained in:
David Baird
2014-01-01 15:46:39 -08:00
2 changed files with 11 additions and 1 deletions
+10 -1
View File
@@ -112,6 +112,7 @@ class _DummyWebSocket : public easywsclient::WebSocket
public: public:
void poll(int timeout) { } void poll(int timeout) { }
void send(const std::string& message) { } void send(const std::string& message) { }
void sendPing() { }
void close() { } void close() { }
void _dispatch(Callback & callable) { } void _dispatch(Callback & callable) { }
readyStateValues getReadyState() const { return CLOSED; } 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) { void send(const std::string& message) {
sendData(wsheader_type::TEXT_FRAME, message);
}
void sendData(wsheader_type::opcode_type type, const std::string& message) {
// TODO: // TODO:
// Masking key should (must) be derived from a high quality random // Masking key should (must) be derived from a high quality random
// number generator, to mitigate attacks on non-WebSocket friendly // number generator, to mitigate attacks on non-WebSocket friendly
@@ -316,7 +325,7 @@ class _RealWebSocket : public easywsclient::WebSocket
std::vector<uint8_t> header; std::vector<uint8_t> header;
uint64_t message_size = message.size(); uint64_t message_size = message.size();
header.assign(2 + (message_size >= 126 ? 2 : 0) + (message_size >= 65536 ? 6 : 0) + (useMask ? 4 : 0), 0); 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) { } if (false) { }
else if (message_size < 126) { else if (message_size < 126) {
header[1] = (message_size & 0xff) | (useMask ? 0x80 : 0); header[1] = (message_size & 0xff) | (useMask ? 0x80 : 0);
+1
View File
@@ -26,6 +26,7 @@ class WebSocket {
virtual ~WebSocket() { } virtual ~WebSocket() { }
virtual void poll(int timeout = 0) = 0; // timeout in milliseconds virtual void poll(int timeout = 0) = 0; // timeout in milliseconds
virtual void send(const std::string& message) = 0; virtual void send(const std::string& message) = 0;
virtual void sendPing() = 0;
virtual void close() = 0; virtual void close() = 0;
virtual readyStateValues getReadyState() const = 0; virtual readyStateValues getReadyState() const = 0;
template<class Callable> template<class Callable>