me-no-dev / ESPAsyncWebServer

Async Web Server for ESP8266 and ESP32
3.78k stars 1.23k forks source link

[Solved] AsyncWebsocketClient send and ArduinoJson #1339

Closed softwarecrash closed 1 year ago

softwarecrash commented 1 year ago

Hello,

i have tried the example

void sendDataWs(AsyncWebSocketClient * client)
{
    DynamicJsonBuffer jsonBuffer;
    JsonObject& root = jsonBuffer.createObject();
    root["a"] = "abc";
    root["b"] = "abcd";
    root["c"] = "abcde";
    root["d"] = "abcdef";
    root["e"] = "abcdefg";
    size_t len = root.measureLength();
    AsyncWebSocketMessageBuffer * buffer = ws.makeBuffer(len); //  creates a buffer (len + 1) for you.
    if (buffer) {
        root.printTo((char *)buffer->get(), len + 1);
        if (client) {
            client->text(buffer);
        } else {
            ws.textAll(buffer);
        }
    }
}

but it looks something has changed in arduinoJson 6, can anyone provide me a actual example for a dynamicjson document and write direct to the buffer? thanks

softwarecrash commented 1 year ago

have found the solution

just for other:

    size_t len = measureJson(liveJson);
    AsyncWebSocketMessageBuffer *buffer = ws.makeBuffer(len);
    if (buffer)
    {
      serializeJson(liveJson, (char *)buffer->get(), len + 1);
      wsClient->text(buffer);
    }