Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Open
ScreamZ opened this issue Jan 5, 2024 · 2 comments

Comments

@ScreamZ
Copy link

ScreamZ commented Jan 5, 2024

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?

bool WebSocketsClient::sendTXT(uint8_t * payload, size_t length, bool headerToPayload) {
if(length == 0) {
length = strlen((const char *)payload);
}
if(clientIsConnected(&_client)) {
return sendFrame(&_client, WSop_text, payload, length, true, headerToPayload);
}
return false;
}

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

@Links2004
Copy link
Owner

Links2004 commented Jan 5, 2024

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:

virtual void runCbEvent(WStype_t type, uint8_t * payload, size_t length) {
if(_cbEvent) {
_cbEvent(type, payload, length);
}
}

runCbEvent(type, payload, length);
}

void WebSockets::handleWebsocketPayloadCb(WSclient_t * client, bool ok, uint8_t * payload) {

readCb(client, payload, header->payloadLen, std::bind(&WebSockets::handleWebsocketPayloadCb, this, std::placeholders::_1, std::placeholders::_2, payload));
} else {
handleWebsocketPayloadCb(client, true, NULL);
}

void WebSockets::handleWebsocket(WSclient_t * client) {

void WebSocketsClient::handleClientData(void) {

void WebSocketsClient::loop(void) {

@ScreamZ
Copy link
Author

ScreamZ commented Jan 5, 2024

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

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
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants