gilmaimon/ArduinoWebsockets

client problem when received messages bigger than 36k.

Opened this issue · 2 comments

Describe the bug
not able to receive a message longer than 36k

To Reproduce
basically the server sends a payload after a request, at some point (36k) the message is not processed anymore.

This should include:

  • The library version you are using: 0.5.0
  • The board you are using: ESP32
  • Information about other components in the system (do you use third party client/server software?)
    I am testing this using the example sketch ESP32-client.ino
    Expected behavior
    To be able to open the message
    Code

`#include <ArduinoWebsockets.h>
#include <WiFi.h>

int xx = ESP.getFreeHeap();
const char* ssid = "xx"; //Enter SSID
const char* password = "xx"; //Enter Password
const char* websockets_server_host = "10.0.1.101";
const uint16_t websockets_server_port = 4444; // Enter server port

using namespace websockets;

WebsocketsClient client;
void setup() {
Serial.begin(115200);
Serial.print("principio: "); Serial.println(xx);
Serial.print("setup: "); Serial.println(ESP.getFreeHeap());
// Connect to wifi
WiFi.begin(ssid, password);

// Wait some time to connect to wifi
for(int i = 0; i < 10 && WiFi.status() != WL_CONNECTED; i++) {
    Serial.print(".");
    delay(1000);
}

// Check if connected to wifi
if(WiFi.status() != WL_CONNECTED) {
    Serial.println("No Wifi!");
    return;
}

Serial.println("Connected to Wifi, Connecting to server.");
// try to connect to Websockets server
bool connected = client.connect(websockets_server_host, websockets_server_port, "/");
if(connected) {
    Serial.println("Connected!");
    client.send("Hello Server");
} else {
    Serial.println("Not Connected!");
}

// run callback when messages are received
client.onMessage([&](WebsocketsMessage msg){
Serial.print("client.onMessage([&](WebsocketsMessage msg al principio: "); Serial.println(ESP.getFreeHeap());
Serial.println("Got Message:-----------------------------------------");

// Serial.println(msg.data());
Serial.println("End of Message---------------------------------------");
Serial.printf("Message T%d B%d Pi%d Po%d C%d stream%d length: %u\n", msg.isText(), msg.isBinary(), msg.isPing(), msg.isPong(), msg.isClose(),msg.isPartial(),msg.rawData().length());
Serial.println(msg.c_str());

});
getSceneList();

}

void loop() {
// let the websockets client check for incoming messages
if(client.available()) {
client.poll();
}
delay(500);
}

void getSceneList(){
String req ="{"message-id":"11","request-type":"GetSceneList"}";
int messageId = 11;
SendRequest(req);
Serial.println("Sent request getSceneList message ID 11:");
}

void SendRequest(String req){
client.send(req); //WORKS

}`

Additional context

error message at serial :

Message T1 B0 Pi0 Po0 C0 stream0 length: 62
{
"error": "invalid JSON payload",
"status": "error"
}
abort() was called at PC 0x40133ba3 on core 1

Backtrace: 0x4008c434:0x3ffb1cb0 0x4008c665:0x3ffb1cd0 0x40133ba3:0x3ffb1cf0 0x40133bea:0x3ffb1d10 0x40133c97:0x3ffb1d30 0x40133d16:0x3ffb1d50 0x40134175:0x3ffb1d70 0x401345e7:0x3ffb1d90 0x40134631:0x3ffb1dc0 0x400d2a46:0x3ffb1de0 0x400d2a75:0x3ffb1e00 0x400d3416:0x3ffb1e70 0x400d346d:0x3ffb1e90 0x400d0f25:0x3ffb1f90 0x400d6b21:0x3ffb1fb0 0x40088b7d:0x3ffb1fd0

Rebooting...
ets Jun 8 2016 00:22:57

NOTE:
If i reduce the size of the message a little bit then I can open it.
For example this is the same message but smaller and no issues:

Message T1 B0 Pi0 Po0 C0 stream0 length: 36709
{
"current-scene": "Welcome 4 2 2 2",
"message-id": "11",
"scenes": [
{

.....
Message with problems when opening:
message.txt

You can try using an esp32 stack-trace decoder to see where exactly the code aborts. I assume maybe allocating 36K is too much to malloc for the poor esp32. The memory handling in this library is not optimal, although it did improve greatly since the initial versions. With the stacktrace decoded it might be easier to understand where it crashes and maybe there is a way to optimize that code. Also, note that the decoded stacktrace might contain personal paths in your computer, so if you share it (which will really help!) make sure to censor those :)

Gil

Thanks, I'll have a look at esp32 stack-trace decoder.