mathieucarbou / ESPAsyncWebServer

Asynchronous HTTP and WebSocket Server Library for ESP32, ESP8266 and RP2040
https://mathieu.carbou.me/ESPAsyncWebServer/
GNU Lesser General Public License v3.0
74 stars 16 forks source link

missing overload: void text(uint32_t id, std::shared_ptr<std::vector<uint8_t>> buffer); #27

Closed tueddy closed 5 months ago

tueddy commented 5 months ago

The optimized websocket delivery works fine but I miss a possibility to notify a single client with optimized delivery. Can you maybe add an overlad function fortext() same as textAll()?

void text(uint32_t id, std::shared_ptr<std::vector> buffer);

Current:

    void text(uint32_t id, const uint8_t * message, size_t len);
    void text(uint32_t id, const char *message, size_t len);
    void text(uint32_t id, const char *message);
    void text(uint32_t id, const String &message);
    void text(uint32_t id, const __FlashStringHelper *message);

Maybe something like this:

void AsyncWebSocket::text(uint32_t id, std::shared_ptr<std::vector<uint8_t>> buffer)
{
    if (AsyncWebSocketClient * c = client(id))
        c->text(buffer);
}

Thanks & best regards Dirk

mathieucarbou commented 5 months ago

Of course! Will do it quickly in a few minutes ;-)

tueddy commented 5 months ago

Do not rush ;-)

mathieucarbou commented 5 months ago

Note that I strongly avise you to use the AsyncWebSocketMessageBuffer class instead, like in the spirit of the original fork.

std::shared_ptr<std::vector<uint8_t>> was introduced in yubox-node-org fork as an internal way of fixing the pointers scope / references, but this should be kept internal and not be dealt with. So I added back the AsyncWebSocketMessageBuffer which wraps that without overhead.

mathieucarbou commented 5 months ago

mathieucarbou/ESP Async WebServer @ 2.10.2 is released and in PlatformIO registry ;-)

tueddy commented 5 months ago

@mathieucarbou Thank's for the fast response & update! Everything is working now:

#if defined(ASYNCWEBSERVER_FORK_mathieucarbou)
    // serialize JSON in a more optimized way using a shared buffer
    const size_t len = measureJson(doc);
    auto buffer = std::make_shared<std::vector<uint8_t>>(len);
    serializeJson(doc, buffer->data(), len);
    if (client == 0) {
        ws.textAll(std::move(buffer));
    } else {
        ws.text(client, std::move(buffer));
    }
#else
..

I would agree to use a higher level thing like AsyncWebSocketMessageBuffer but your example on the webpage shows implementation like above. Do you have a suggestion to rewrite code above for AsyncWebSocketMessageBuffer and apply the example on the documentation page? Please note if need booth, text() & textAll() without copying data around..

Thank's & best regards Dirk