From 4b52beee686505b65a86c3942ef91a8d4a15b255 Mon Sep 17 00:00:00 2001 From: Gil Maimon Date: Fri, 22 Nov 2019 11:36:41 +0200 Subject: [PATCH 1/3] messages now have rawData, c_str as additional accessors (together with data) which should solve completly the issue with binary data not working propertly (meaning, using c_str or rawData should not make any convertions and return the proper data --- src/tiny_websockets/message.hpp | 9 ++++++--- 1 file changed, 6 insertions(+), 3 deletions(-) diff --git a/src/tiny_websockets/message.hpp b/src/tiny_websockets/message.hpp index 653321e..c4edaf3 100644 --- a/src/tiny_websockets/message.hpp +++ b/src/tiny_websockets/message.hpp @@ -19,7 +19,7 @@ namespace websockets { // The class the user will interact with as a message // This message can be partial (so practically this is a Frame and not a message) struct WebsocketsMessage { - WebsocketsMessage(MessageType msgType, WSString msgData, MessageRole msgRole = MessageRole::Complete) : _type(msgType), _length(msgData.size()), _data(internals::fromInternalString(msgData)), _role(msgRole) {} + WebsocketsMessage(MessageType msgType, const WSString& msgData, MessageRole msgRole = MessageRole::Complete) : _type(msgType), _length(msgData.size()), _data(msgData), _role(msgRole) {} WebsocketsMessage() : WebsocketsMessage(MessageType::Empty, "", MessageRole::Complete) {} static WebsocketsMessage CreateFromFrame(internals::WebsocketsFrame frame, MessageType overrideType = MessageType::Empty) { @@ -68,7 +68,10 @@ namespace websockets { bool isLast() const { return this->_role == MessageRole::Last; } - WSInterfaceString data() const { return this->_data; } + WSInterfaceString data() const { return internals::fromInternalString(this->_data); } + const WSString& rawData() const { return this->_data; } + const char* c_str() const { return this->_data.c_str(); } + uint32_t length() const { return this->_length; } class StreamBuilder { @@ -177,7 +180,7 @@ namespace websockets { private: const MessageType _type; const uint32_t _length; - const WSInterfaceString _data; + const WSString _data; const MessageRole _role; }; } \ No newline at end of file From e0e526fdbe5f46e089849d59cd3377c7fbda9c5f Mon Sep 17 00:00:00 2001 From: Gil Maimon Date: Fri, 22 Nov 2019 12:17:20 +0200 Subject: [PATCH 2/3] Added new keywords to keywords.txt --- keywords.txt | 2 ++ 1 file changed, 2 insertions(+) diff --git a/keywords.txt b/keywords.txt index 59f2d60..e4d97ff 100644 --- a/keywords.txt +++ b/keywords.txt @@ -45,6 +45,8 @@ isLast KEYWORD2 WebsocketsMessage KEYWORD1 data KEYWORD2 type KEYWORD2 +rawData KEYWORD2 +c_str KEYWORD2 WebsocketsEvent KEYWORD1 ConnectionOpened LITERAL1 From 61ca151650168abb87eb20a72ec70a02356da62e Mon Sep 17 00:00:00 2001 From: Gil Maimon Date: Fri, 22 Nov 2019 12:34:50 +0200 Subject: [PATCH 3/3] Fixed issue by adding c_str and rawData as accessors in WebsocketsMessage #32 --- README.md | 10 +++++++++- library.properties | 2 +- 2 files changed, 10 insertions(+), 2 deletions(-) diff --git a/README.md b/README.md index ebfa6d1..0854f3d 100644 --- a/README.md +++ b/README.md @@ -176,6 +176,13 @@ void loop() { ``` ***Note:** for ESP32 you only need to change to code that connects to WiFi (replace `#include ` with `#include `), everything else stays the same.* +## Binary Data + +For binary data it is recommended to use `msg.rawData()` which returns a `std::string`, or `msg.c_str()` which returns a `const char*`. +The reason is that `msg.data()` returns an Arduino `String`, which is great for Serial printing and very basic memory handling but bad for most binary usages. + +See [issue #32](https://github.com/gilmaimon/ArduinoWebsockets/issues/32) for further information. + ## SSL and WSS Support No matter what board you are using, in order to use WSS (websockets over SSL) you need to use @@ -285,4 +292,5 @@ Thanks for everyone who reported a bug, suggested a feature and contributed to t - **10/08/2019 (v0.4.10)** - Patch - Bugfix. Fixed a bug (and general in-stability) caused from unchecked and unsafe read operations on sockets. Also improved memory usage and management. Thank you [Jonty](https://github.com/Jonty) for openning and helping with the [issue](https://github.com/gilmaimon/ArduinoWebsockets/issues/26)! - **14/09/2019 (v0.4.11)** - Bugfixes - masking settings used to not get copied when using assignment between `WebsocketClient` instances. Also handshake validation is now case insensitive. Thank you [logdog2709](https://github.com/logdog2709) for pointing out the [issue](https://github.com/gilmaimon/ArduinoWebsockets/issues/34). - **12/10/2019 (v0.4.12)** - Patch - Messages are now sent as a single TCP buffer instead of separate messages. Thank you [elC0mpa](https://github.com/elC0mpa) for posting the [issue](https://github.com/gilmaimon/ArduinoWebsockets/issues/44). -- **19/10/2019 (v0.4.13)** - Patch - added `yield` calls in order to prevent software-watchdog resets on esp8266 (on long messages). Thank you [elC0mpa](https://github.com/elC0mpa) for documenting and helping with the [issue](https://github.com/gilmaimon/ArduinoWebsockets/issues/43). \ No newline at end of file +- **19/10/2019 (v0.4.13)** - Patch - added `yield` calls in order to prevent software-watchdog resets on esp8266 (on long messages). Thank you [elC0mpa](https://github.com/elC0mpa) for documenting and helping with the [issue](https://github.com/gilmaimon/ArduinoWebsockets/issues/43). +- **22/11/2019 (v0.4.14)** - Added `rawData` and `c_str` as acccessors in `WebsocketsMessage` so now the raw data can be acccessed which should solve issue #32 and not break any existing sketch. \ No newline at end of file diff --git a/library.properties b/library.properties index d6a72a1..62b24d5 100644 --- a/library.properties +++ b/library.properties @@ -1,5 +1,5 @@ name=ArduinoWebsockets -version=0.4.13 +version=0.4.14 author=Gil Maimon maintainer=Gil Maimon sentence=A library for writing modern Websockets applications with Arduino.