arduinoWebSockets icon indicating copy to clipboard operation
arduinoWebSockets copied to clipboard

Can we describe the behavior of disconnect/autoreconnect and according action

Open ScreamZ opened this issue 6 months ago • 2 comments

Hi,

I would be pleased to improve the documentation on various points about connection stability if you could anyone help me with the investigation.

Once the first authentication has been done we set autoReconnectInterval which is by default 500ms.

1 - What happens if we use webSocket.sendTXT if the client is disconnected? Does it crash, enqueue, ignore ?

It seems that it returns false nothing gets queued, but there is no crash. Is that right? https://github.com/Links2004/arduinoWebSockets/blob/30d5e136665a52880f641ddd7245b3ba05ecd32b/src/WebSocketsClient.cpp#L303-L311

2 - is webSocket.onEvent called by webSocket.loop and therefore it's blocking the loop while loading

ScreamZ avatar Jan 05 '24 11:01 ScreamZ

Hi, more documentation is very welcome.

  1. you are correct, no queued, and the return value indecates if somthing is send.
  2. yes webSocket.loop is calling webSocket.onEvent (unless tcp async is used)

the call stack look like this:

https://github.com/Links2004/arduinoWebSockets/blob/30d5e136665a52880f641ddd7245b3ba05ecd32b/src/WebSocketsClient.h#L162-L166

https://github.com/Links2004/arduinoWebSockets/blob/30d5e136665a52880f641ddd7245b3ba05ecd32b/src/WebSocketsClient.cpp#L467-L469

https://github.com/Links2004/arduinoWebSockets/blob/30d5e136665a52880f641ddd7245b3ba05ecd32b/src/WebSockets.cpp#L468

https://github.com/Links2004/arduinoWebSockets/blob/30d5e136665a52880f641ddd7245b3ba05ecd32b/src/WebSockets.cpp#L462-L465

https://github.com/Links2004/arduinoWebSockets/blob/30d5e136665a52880f641ddd7245b3ba05ecd32b/src/WebSockets.cpp#L341-L341

https://github.com/Links2004/arduinoWebSockets/blob/30d5e136665a52880f641ddd7245b3ba05ecd32b/src/WebSocketsClient.cpp#L558

https://github.com/Links2004/arduinoWebSockets/blob/30d5e136665a52880f641ddd7245b3ba05ecd32b/src/WebSocketsClient.cpp#L202

Links2004 avatar Jan 05 '24 12:01 Links2004

Alright, I'll work on some documentation. I'll also link issues here as long as I find some.

  • https://github.com/Links2004/arduinoWebSockets/issues/864#issue-2067453463

I'll write in doc for now that this is safe to just call websocket.loop() in the loop. As the send method is only doing a skip and false.

Also, the user can implement queuing on its own if desired.

Here is my current implementation of sending, which just skip with a message printed.

void serializeAndSend(WebSocketsClient &webSocket, const JsonDocument &document)
{
  // Serialize the JSON document to a String
  String jsonString;
  serializeJson(document, jsonString);

  // Send the JSON message to the WebSocket server
#ifdef DEBUG
  const bool isSent = webSocket.sendTXT(jsonString);

  Serial.print("📥 OUT - ");
  if (isSent)
    Serial.println(jsonString);
  else
    Serial.println("Not connected… Skipped!");
#else
  webSocket.sendTXT(jsonString);
#endif
}

ScreamZ avatar Jan 05 '24 14:01 ScreamZ