From 0ceba03b6b3272c81817f99222b364f3687b4b78 Mon Sep 17 00:00:00 2001 From: "adam@wssc.vic.edu.au" Date: Thu, 25 Jun 2015 20:21:47 +1000 Subject: [PATCH] Websocket send binary data back to client --- Sming/SmingCore/Network/WebSocket.cpp | 10 ++++++++-- Sming/SmingCore/Network/WebSocket.h | 4 +++- 2 files changed, 11 insertions(+), 3 deletions(-) diff --git a/Sming/SmingCore/Network/WebSocket.cpp b/Sming/SmingCore/Network/WebSocket.cpp index 369b31c6e9..8eee1a6e3a 100644 --- a/Sming/SmingCore/Network/WebSocket.cpp +++ b/Sming/SmingCore/Network/WebSocket.cpp @@ -36,11 +36,11 @@ bool WebSocket::initialize(HttpRequest& request, HttpResponse& response) return true; } -void WebSocket::send(const char* message, int length) +void WebSocket::send(const char* message, int length, wsFrameType type) { uint8_t frameHeader[16] = {0}; size_t headSize = sizeof(frameHeader); - wsMakeFrame(NULL, length, frameHeader, &headSize, WS_TEXT_FRAME); + wsMakeFrame(NULL, length, frameHeader, &headSize, type); connection->write((char*)frameHeader, headSize, TCP_WRITE_FLAG_COPY | TCP_WRITE_FLAG_MORE); connection->writeString(message, TCP_WRITE_FLAG_COPY); connection->flush(); @@ -50,3 +50,9 @@ void WebSocket::sendString(const String& message) { send(message.c_str(), message.length()); } + +void WebSocket::sendBinary(const uint8_t* data, int size) +{ + send((char*)data, size, WS_BINARY_FRAME); +} + diff --git a/Sming/SmingCore/Network/WebSocket.h b/Sming/SmingCore/Network/WebSocket.h index d13c1323ac..bac6181d95 100644 --- a/Sming/SmingCore/Network/WebSocket.h +++ b/Sming/SmingCore/Network/WebSocket.h @@ -13,6 +13,7 @@ #include "../../Wiring/WHashMap.h" #include "../../Wiring/WVector.h" #include "../Delegate.h" +#include "../../Libraries/cWebsocket/websocket.h" class HttpServer; @@ -21,8 +22,9 @@ class WebSocket friend class HttpServer; public: WebSocket(HttpServerConnection* conn); - virtual void send(const char* message, int length); + virtual void send(const char* message, int length, wsFrameType type = WS_TEXT_FRAME); void sendString(const String& message); + void sendBinary(const uint8_t* data, int size); protected: bool initialize(HttpRequest &request, HttpResponse &response);