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

ESP32/WebSocketClient seems to block for a long time in case of unstable and very slow network connections #911

Open
mirkosertic opened this issue Oct 2, 2024 · 1 comment

Comments

@mirkosertic
Copy link

I'm using the WebSocketClient with an ESP32. I know that I have a fairly slow and sometimes unstable WiFi connectivity.

Currently it seems that the sendBIN() methods are blocking for a very long time, and is not giving up. Is there a way to indicate a kind of send-timeout for such invocations? I really would like to give up in case of 1000 bytes or so cannot be transferred in under one second.

I know that there is the WEBSOCKETS_TCP_TIMEOUT in 'WebSockets.h', but I don't know if this is the place to configure the send-timeout in case of very slow and unstable connections.

@Links2004
Copy link
Owner

Links2004 commented Oct 2, 2024

all TCP writes in the lib go via this function:

/**
* write x byte to tcp or get timeout
* @param client WSclient_t *
* @param out uint8_t * data buffer
* @param n size_t byte count
* @return bytes send
*/
size_t WebSockets::write(WSclient_t * client, uint8_t * out, size_t n) {
if(out == NULL)
return 0;
if(client == NULL)
return 0;
unsigned long t = millis();
size_t len = 0;
size_t total = 0;
DEBUG_WEBSOCKETS("[write] n: %zu t: %lu\n", n, t);
while(n > 0) {
if(client->tcp == NULL) {
DEBUG_WEBSOCKETS("[write] tcp is null!\n");
break;
}
if(!client->tcp->connected()) {
DEBUG_WEBSOCKETS("[write] not connected!\n");
break;
}
if((millis() - t) > WEBSOCKETS_TCP_TIMEOUT) {
DEBUG_WEBSOCKETS("[write] write TIMEOUT! %lu\n", (millis() - t));
break;
}
len = client->tcp->write((const uint8_t *)out, n);
if(len) {
t = millis();
out += len;
n -= len;
total += len;
// DEBUG_WEBSOCKETS("write %d left %d!\n", len, n);
} else {
DEBUG_WEBSOCKETS("WS write %d failed left %d!\n", len, n);
}
if(n > 0) {
WEBSOCKETS_YIELD();
}
}
WEBSOCKETS_YIELD();
return total;
}

the but the "blocking" you describe is most likely on the lower layers (TCP stack of the ESP client->tcp->write), an not in this library.
the WEBSOCKETS_TCP_TIMEOUT define can only work when the TCP stack gives back the control to the code of the library and this can take more time then the define says.

the TCP stack is informed about the timeout we want via this code here:

if(_client.tcp->connect(_host.c_str(), _port, WEBSOCKETS_TCP_TIMEOUT)) {

but if that works in all cases is up to the TCP stack.

enabling the debug output will give you more information on where the code is stuck and waiting.

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