-
Notifications
You must be signed in to change notification settings - Fork 555
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
"case WStype_DISCONNECTED:" never called after the internet connection is lost. #363
Comments
the TCP stack of the ESP can only detect that the connection is dead when you try to send something. WiFiMulti.run() == WL_CONNECTED will prevent the webSocket.sendTXT("WS TEXT TO SEND"); so you never call the send function --> result the ESP TCP stack can not detect that the socket is down --> no event. can you try to call sendTXT every time and not only when the WiFi state is WL_CONNECTED? |
@Links2004 Hi, Thanks for your answer.
So yes, I call the send function:
What my code does:
My code 8 times executed webSocket.sendTXT("WS TEXT TO SEND"); but
That is the problem. WiFi state is WL_CONNECTED even if my internet is down. |
ok now I get it. |
@Links2004 ok, no problem. When you see
|
ok its clearly a TCP layer problem, on of two thinks can be the problem:
|
@Links2004 Ok, I use OpenWRT on my router. Here are Wireshark packets: My second router at 192.168.3.1, on which I disconnect the internet, sends "destination unreachable". #373 Similar |
I tried adding |
I can confirm the callback is not being called. It seems WebSocketsClient.cpp immediately tries to reconnect and never calls the disconnected callback. This can be seen by enabling debugging for ArduinoWebsockets and ESPAsyncTCP, which shows the async onDisconnect callback firing:
The asyncConnect fires very rapidly, every few milliseconds. Here WebSocketClient.cpp line 694 handles the async onDisconnect callback, but does not trigger the I tested this solution and it worked well:
.....
bool cHttpHeadersValid; ///< non-websocket http header validity indicator
size_t cMandatoryHeadersCount; ///< non-websocket mandatory http headers present count
#if (WEBSOCKETS_NETWORK_TYPE == NETWORK_ESP8266_ASYNC)
String cHttpLine; ///< HTTP header lines
bool cWasDisconnected;
#endif
} WSclient_t;
......
void WebSocketsClient::connectedCb() {
DEBUG_WEBSOCKETS("[WS-Client] connected to %s:%u.\n", _host.c_str(), _port);
#if (WEBSOCKETS_NETWORK_TYPE == NETWORK_ESP8266_ASYNC)
_client.tcp->onDisconnect(std::bind([](WebSocketsClient * c, AsyncTCPbuffer * obj, WSclient_t * client) -> bool {
DEBUG_WEBSOCKETS("[WS-Server][%d] Disconnect client\n", client->num);
client->status = WSC_NOT_CONNECTED;
client->tcp = NULL;
client->cWasDisconnected = true; //Tell the asyncConnect() function to call the DISCONECTED callback
// reconnect
c->asyncConnect();
return true;
}, this, std::placeholders::_1, &_client));
#endif
_client.status = WSC_HEADER;
#if (WEBSOCKETS_NETWORK_TYPE != NETWORK_ESP8266_ASYNC)
// set Timeout for readBytesUntil and readStringUntil
_client.tcp->setTimeout(WEBSOCKETS_TCP_TIMEOUT);
#endif
#if (WEBSOCKETS_NETWORK_TYPE == NETWORK_ESP8266)
_client.tcp->setNoDelay(true);
if(_client.isSSL && _fingerprint.length()) {
if(!_client.ssl->verify(_fingerprint.c_str(), _host.c_str())) {
DEBUG_WEBSOCKETS("[WS-Client] certificate mismatch\n");
WebSockets::clientDisconnect(&_client, 1000);
return;
}
}
#endif
// send Header to Server
sendHeader(&_client);
}
void WebSocketsClient::asyncConnect() {
DEBUG_WEBSOCKETS("[WS-Client] asyncConnect...\n");
//Call the callback if the client was just disconnected
if (_client.cWasDisconnected) {
runCbEvent(WStype_DISCONNECTED, NULL, 0);
_client.cWasDisconnected = false;
}
AsyncClient * tcpclient = new AsyncClient();
.....
} |
Serial
As you can see after the connection is lost,
case WStype_DISCONNECTED:
is not called. The sketch 8 times tried to send text via WebSocket using:webSocket.sendTXT("WS TEXT TO SEND");
but nothing...But eventually, it gets called... but only after connection to internet is re-established.
As you can see I connected the internet after 4 minus and only after that "disconnect" gets called. Why so?
The text was updated successfully, but these errors were encountered: