Skip to content
This repository has been archived by the owner on Jan 21, 2025. It is now read-only.

Websocket com not correctly working #176

Closed
mathieucarbou opened this issue Dec 18, 2024 · 2 comments · Fixed by #179
Closed

Websocket com not correctly working #176

mathieucarbou opened this issue Dec 18, 2024 · 2 comments · Fixed by #179
Assignees
Labels

Comments

@mathieucarbou
Copy link
Owner

I have a similar issue after switching from esphome/ESPAsyncWebServer-esphome.
The issues is that socket.textAll() doesn't seem to work in a socket event handler.

Code looks like this:

AsyncWebSocket websocket("/ws");
int clients_connected = 0;
String test_msg = "A new client connected!";

// Socket event handler
void onSocketEvent(AsyncWebSocket* server, AsyncWebSocketClient* client, AwsEventType type, void* arg, uint8_t* data, size_t len) {
  switch (type) {
    case WS_EVT_CONNECT:
      ++clients_connected;

      websocket.textAll(test_msg);   // This message is not received by clients.

      // This is being executed correctly when a new client connects
      Serial.print("[Websocket] Client ");
      Serial.print(client->id());
      Serial.print(" connected with IP: ");
      Serial.println(client->remoteIP());
      break;

    case WS_EVT_DISCONNECT:
        --clients_connected;
     .
     .
     .
  }
}

// Called in setup()
void initWebsocket() {
  websocket.onEvent(onSocketEvent);
}

With the esphome branch of ESPAsyncWebserver this worked fine with multiple clients connected. The message/s sent are also very small. It is only in the event handler that websocket.textAll() doesn't work. In the loop() it works just fine.

I tried both AsyncTCP and AsyncTCPSock.

Originally posted by @stefan-wr in #65 (reply in thread)

@mathieucarbou mathieucarbou self-assigned this Dec 18, 2024
@stefan-wr
Copy link

stefan-wr commented Dec 19, 2024

I tested a few older versions down to

mathieucarbou/Async TCP @ 3.0.0
mathieucarbou/ESP Async WebServer @ 2.4.1

The issues persists.

But I found out that it only affects the new socket client that just connected and caused the WS_EVT_CONNECTED event type.
All other previously connected clients receive the message. For example:
Client 1 and Client 2 are connected to websocket.
Now a third client, Client 3 connects and causes the text_msg to be sent from inside the event handler to all clients, but it is only received by Client 1 & 2.

If I send the message directly to the client from within the event handler, e.g.

client->text(test_msg);

the message reaches the client.

@vortigont
Copy link
Collaborator

vortigont commented Dec 19, 2024

It is by design.

AsyncWebSocketClient* AsyncWebSocket::_newClient(AsyncWebServerRequest* request) {
  _clients.emplace_back(request, this);
  return &_clients.back();
}

WS_EVT_CONNECT callback is called from the AsyncWebSocketClient's constructor (which is probably wrong place for user code's callbacks) when object is not yet linked to a list of server's clients.

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.