Skip to content

Getting my connection closed when on node js wss server #163

@dederomagnolo

Description

@dederomagnolo

Hi all and @gilmaimon. First of all, thanks for this lib and for all the support for the community. This is very useful and I could learn A LOT about websockets reading the discussions here.

Describe the bug
I have a problem on my system with node js wss server + esp8266 clients.

my clients enter in a kind of loop when the server is authenticating them and immediately the connection is closed. I can see the close event only on client side and it is returning 1002 (protocols errors).

Technical settings:

  • I have a node JS (14.5.0) server exposing the wss with the lib ws. I connect my esp8266 clients using ArduinoWebsockets (0.5.3) to it.
  • ESP8266 lib version: 3.0.2
  • I noticed that is occuring because of async calls on my authentication function on server side... but I already handled everything to work properly and I am able to test and get success using a web client.
  • My server is live via Render cloud and this error only happens live.
  • From my esp8266 client, I am calling the function below to connect to the server (minimal code) on my loop:
void handleWebsocketConnection(bool startup) {
 // first connection on setup ( )
  if (startup) {
    bool wsConnected = wsclient.connect(SERVER_URI);

    if (wsConnected) {
      serialDebug("Connected with BeThere websocket server:");
      serialDebugLn(SERVER_URI);
    }
    lastConnectionTentative = millis();
  }
  
  if (wsclient.available()) {
    wsclient.poll();
  } else {
   // if the connection is closed, try to reconnect
    bool shouldTryToReconnect = (millis() - lastConnectionTentative) > connectionToWsTimeout;

    if (shouldTryToReconnect) {
      serialDebugLn("...Reconnecting to websocket server");

      wsclient = {}; // This will reset the client object
      wsclient = WebsocketsClient();
      wsclient.onEvent(onEventsCallback);
      wsclient.onMessage(onMessageCallback);
      
      bool wsConnected = wsclient.connect(SERVER_URI);
  
      if (wsConnected) {
        serialDebug("Reconnected with BeThere websocket server:");
      }

      lastConnectionTentative = millis();
    }
  }
}

My suspicion is that my esp8266 client is not reacting well to this authentication function cause I am not handling it correctly because when I pull off one of the async calls I do, they are able to connect and to keep the connection estable. I tried to add this timer shouldTryToReconnect to make the client wait 5 seconds before another tentative but without success.. Can you help me to understand what I am doing wrong?

Metadata

Metadata

Assignees

Labels

No labels
No labels

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions