Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

websocket (WS_EVT_DATA) event from client does not update state on esp8266 only #307

Open
tichaonax opened this issue Jun 24, 2022 · 1 comment

Comments

@tichaonax
Copy link

On esp8266 the event handler from file

lib\framework\WebSocketTxRx.h

is not passed the correct size_t len. As a result the if condition to proceed with updating the state is never executed. This works well on esp32 and I cannot figure out if its a library issue or not.
I have a json size 1365 characters but the len passed is 528.

  virtual void onWSEvent(AsyncWebSocket* server,
                         AsyncWebSocketClient* client,
                         AwsEventType type,
                         void* arg,
                         uint8_t* data,
                         size_t len) {
    if (type == WS_EVT_DATA) {
      Serial.println(F("WS_EVT_DATA"));
      AwsFrameInfo* info = (AwsFrameInfo*)arg;
      if (info->final && info->index == 0 && info->len == len) {

lib_deps =
  ArduinoJson@>=6.0.0,<7.0.0
  ; The following allows the use of the latest code for ESPAsyncWebServer - there hasn't been a release in a while
  ; Work around for https://github.com/me-no-dev/ESPAsyncWebServer/issues/1151
  https://github.com/me-no-dev/ESPAsyncWebServer
  ;ESP Async WebServer@>=1.2.0,<2.0.0
  AsyncMqttClient@>=0.9.0,<1.0.0
@king2
Copy link

king2 commented Aug 3, 2022

Same with ESP32, it called with:

  • info->final = 1
  • info->index = 0
  • info->len = 1605
  • len = 1428

then again, with:

  • info->final = 1
  • info->index = 1428
  • info->len = 1605
  • len = 177

So, **info->len ** if a length of whole message, len is how many bytes was received this time, info->index is index of data received in message.

I'm not sure what info->final is (both times it is 1, I expected it to be 0 in first chunk and 1 in last), but this function waits for complete message in one chunk, and cannot assemble message from two or more parts.

I have added

#define RCV_BUFFER_SIZE 4096
unsigned char rcv_buffer[RCV_BUFFER_SIZE];

just before onWSEvent function and modified its content:

 if (info->len > RCV_BUFFER_SIZE) return;
 memcpy(rcv_buffer + info->index, data, len);
 if ((info->index + len) == info->len) {
     if (info->opcode == WS_TEXT) {
         DynamicJsonDocument jsonDocument = DynamicJsonDocument(WebSocketConnector<T>::_bufferSize);
         DeserializationError error = deserializeJson(jsonDocument, (char*)rcv_buffer);
         if (!error && jsonDocument.is<JsonObject>()) {
             JsonObject jsonObject = jsonDocument.as<JsonObject>();
             WebSocketConnector<T>::_statefulService->update(
             jsonObject, _stateUpdater, WebSocketConnector<T>::clientId(client));
         }
     }
 }

so it can assemble message in separate buffer before deserialization in cost of memory usage.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants