diff --git a/ixwebsocket/IXWebSocket.cpp b/ixwebsocket/IXWebSocket.cpp index 864482ea..03e985f4 100644 --- a/ixwebsocket/IXWebSocket.cpp +++ b/ixwebsocket/IXWebSocket.cpp @@ -429,6 +429,16 @@ namespace ix return (binary) ? sendBinary(data, onProgressCallback) : sendText(data, onProgressCallback); } + WebSocketSendInfo WebSocket::sendBinary(const std::vector& data, + const OnProgressCallback& onProgressCallback) + { + if (!isConnected()) return WebSocketSendInfo(false); + std::lock_guard lock(_writeMutex); + auto webSocketSendInfo = _ws.sendBinary(data, onProgressCallback); + WebSocket::invokeTrafficTrackerCallback(webSocketSendInfo.wireSize, false); + return webSocketSendInfo; + } + WebSocketSendInfo WebSocket::sendBinary(const std::string& text, const OnProgressCallback& onProgressCallback) { diff --git a/ixwebsocket/IXWebSocket.h b/ixwebsocket/IXWebSocket.h index 0c288cd5..d365fe01 100644 --- a/ixwebsocket/IXWebSocket.h +++ b/ixwebsocket/IXWebSocket.h @@ -74,8 +74,11 @@ namespace ix WebSocketSendInfo send(const std::string& data, bool binary = false, const OnProgressCallback& onProgressCallback = nullptr); + WebSocketSendInfo sendBinary(const std::string& text, const OnProgressCallback& onProgressCallback = nullptr); + WebSocketSendInfo sendBinary(const std::vector& data, + const OnProgressCallback& onProgressCallback = nullptr); WebSocketSendInfo sendText(const std::string& text, const OnProgressCallback& onProgressCallback = nullptr); WebSocketSendInfo ping(const std::string& text); diff --git a/ixwebsocket/IXWebSocketPerMessageDeflateCodec.h b/ixwebsocket/IXWebSocketPerMessageDeflateCodec.h index 194c811f..b4d3687b 100644 --- a/ixwebsocket/IXWebSocketPerMessageDeflateCodec.h +++ b/ixwebsocket/IXWebSocketPerMessageDeflateCodec.h @@ -25,9 +25,8 @@ namespace ix bool compress(const std::vector& in, std::string& out); bool compress(const std::vector& in, std::vector& out); - template bool compressData(const T& in, S& out); - private: + template bool compressData(const T& in, S& out); template bool endsWithEmptyUnCompressedBlock(const T& value); int _flush; diff --git a/ixwebsocket/IXWebSocketTransport.cpp b/ixwebsocket/IXWebSocketTransport.cpp index 15e99324..cec6f4ec 100644 --- a/ixwebsocket/IXWebSocketTransport.cpp +++ b/ixwebsocket/IXWebSocketTransport.cpp @@ -751,11 +751,28 @@ namespace ix return static_cast(seconds); } - template WebSocketSendInfo WebSocketTransport::sendData(wsheader_type::opcode_type type, - const T& message, - bool compress, - const OnProgressCallback& onProgressCallback) + const std::string& message, + bool compress, + const OnProgressCallback& onProgressCallback) + { + return sendRawData(type, message, compress, onProgressCallback); + } + + WebSocketSendInfo WebSocketTransport::sendData(wsheader_type::opcode_type type, + const std::vector& message, + bool compress, + const OnProgressCallback& onProgressCallback) + { + return sendRawData(type, message, compress, onProgressCallback); + } + + + template + WebSocketSendInfo WebSocketTransport::sendRawData(wsheader_type::opcode_type type, + const T& message, + bool compress, + const OnProgressCallback& onProgressCallback) { if (_readyState != ReadyState::OPEN && _readyState != ReadyState::CLOSING) { @@ -769,9 +786,11 @@ namespace ix auto message_begin = message.begin(); auto message_end = message.end(); +#if 0 if (compress) { - if (!_perMessageDeflate->compress(message, _compressedMessage)) + T compressedMessage; + if (!_perMessageDeflate->compress(message, compressedMessage)) { bool success = false; compressionError = true; @@ -780,11 +799,12 @@ namespace ix return WebSocketSendInfo(success, compressionError, payloadSize, wireSize); } compressionError = false; - wireSize = _compressedMessage.size(); + wireSize = compressedMessage.size(); - message_begin = _compressedMessage.begin(); - message_end = _compressedMessage.end(); + message_begin = compressedMessage.begin(); + message_end = compressedMessage.end(); } +#endif { std::lock_guard lock(_txbufMutex); @@ -810,8 +830,8 @@ namespace ix // auto steps = wireSize / kChunkSize; - std::string::const_iterator begin = message_begin; - std::string::const_iterator end = message_end; + auto begin = message_begin; + auto end = message_end; for (uint64_t i = 0; i < steps; ++i) { @@ -964,6 +984,14 @@ namespace ix return info; } + WebSocketSendInfo WebSocketTransport::sendBinary(const std::vector& message, + const OnProgressCallback& onProgressCallback) + + { + return sendData( + wsheader_type::BINARY_FRAME, message, _enablePerMessageDeflate, onProgressCallback); + } + WebSocketSendInfo WebSocketTransport::sendBinary(const std::string& message, const OnProgressCallback& onProgressCallback) diff --git a/ixwebsocket/IXWebSocketTransport.h b/ixwebsocket/IXWebSocketTransport.h index 6d185ad6..0906bbfb 100644 --- a/ixwebsocket/IXWebSocketTransport.h +++ b/ixwebsocket/IXWebSocketTransport.h @@ -86,6 +86,9 @@ namespace ix WebSocketInitResult connectToSocket(std::unique_ptr socket, int timeoutSecs); PollResult poll(); + + WebSocketSendInfo sendBinary(const std::vector& message, + const OnProgressCallback& onProgressCallback); WebSocketSendInfo sendBinary(const std::string& message, const OnProgressCallback& onProgressCallback); WebSocketSendInfo sendText(const std::string& message, @@ -190,7 +193,7 @@ namespace ix std::atomic _enablePerMessageDeflate; std::string _decompressedMessage; - std::string _compressedMessage; + std::vector _compressedMessage; // Used to control TLS connection behavior SocketTLSOptions _socketTLSOptions; @@ -239,12 +242,22 @@ namespace ix bool sendOnSocket(); bool receiveFromSocket(); - template WebSocketSendInfo sendData(wsheader_type::opcode_type type, - const T& message, + const std::string& message, bool compress, const OnProgressCallback& onProgressCallback = nullptr); + WebSocketSendInfo sendData(wsheader_type::opcode_type type, + const std::vector& message, + bool compress, + const OnProgressCallback& onProgressCallback = nullptr); + + template + WebSocketSendInfo sendRawData(wsheader_type::opcode_type type, + const T& message, + bool compress, + const OnProgressCallback& onProgressCallback = nullptr); + template bool sendFragment(wsheader_type::opcode_type type, bool fin, diff --git a/test/IXWebSocketChatTest.cpp b/test/IXWebSocketChatTest.cpp index 8c531079..f308ffce 100644 --- a/test/IXWebSocketChatTest.cpp +++ b/test/IXWebSocketChatTest.cpp @@ -100,6 +100,7 @@ namespace } _webSocket.setUrl(url); + _webSocket.disablePerMessageDeflate(); std::stringstream ss; log(std::string("Connecting to url: ") + url); @@ -188,7 +189,9 @@ namespace void WebSocketChat::sendMessage(const std::string& text) { - _webSocket.sendBinary(encodeMessage(text)); + auto msg = encodeMessage(text); + std::vector data(text.begin(), text.end()); + _webSocket.sendBinary(data); } bool startServer(ix::WebSocketServer& server) diff --git a/ws/ws_connect.cpp b/ws/ws_connect.cpp index 3f4c969b..39a0f179 100644 --- a/ws/ws_connect.cpp +++ b/ws/ws_connect.cpp @@ -216,7 +216,8 @@ namespace ix { if (_binaryMode) { - _webSocket.sendBinary(text); + std::vector data(text.begin(), text.end()); + _webSocket.sendBinary(data); } else {