Links2004 / arduinoWebSockets

arduinoWebSockets
GNU Lesser General Public License v2.1
1.87k stars 553 forks source link

no reconnect if server doesn't respond #564

Closed tobiges closed 3 years ago

tobiges commented 3 years ago

My esp8266 connected to the server and didn't receive any response so it waited in a infinite loop for the server response. I added a header response timeout to prevent this. Is there another way how this could be done? Why does is connect and send the request in the first place when my websocket server is not running?! Anyway there should be a timeout if no response is received and the client should disconnect and try to reconnect as it does on pong timeout.

Links2004 commented 3 years ago

I am not sure in what problem you are running. How can the ESP8266 connect to you server when it is not running?

if the TCP port on your server is not open the connect call will fail and the code will retry after _reconnectInterval (default 500ms). (the connect timeout is hard coded in the Arduino abstraction layer, for the ESP32 it can be configured).

https://github.com/Links2004/arduinoWebSockets/blob/master/src/WebSocketsClient.cpp#L208-L209 https://github.com/Links2004/arduinoWebSockets/blob/master/src/WebSocketsClient.cpp#L159-L161

the only possible way to run in your problem I see is wenn you try to connect to some random open TCP port that will happily accept any TCP traffic (SYN, ACK and SEQ handling and answers) but itself never sends any data traffic. which is a strange server for sure, if that is happening in your use case I can see how the code will simply wait for any traffic from the server or the TCP close (FIN) for ever.

https://github.com/Links2004/arduinoWebSockets/blob/master/src/WebSocketsClient.cpp#L494-L508

tobiges commented 3 years ago

I have a golang server running in WSL and when I stop the server somehow the 8266 still connects and just waits. I also tested it with a esp32 but same behavior. It's strange that the esp8266 is still able to connect but the only solution for me was to implement a header response timeout. Now it simply disconnects if _client.status is still WSC_HEADER after 5 seconds here: https://github.com/Links2004/arduinoWebSockets/blob/7e34a8b2463f0344f01c2539219db35ec4c33bca/src/WebSocketsClient.cpp#L493 I will take a closer look why the TCP port is happily accepting any TCP traffic. Maybe you should consider adding a header response timeout just to also account for this strange behavior.

Links2004 commented 3 years ago

yes, having a timeout in the WSC_HEADER state is a good idea. but I am curious how its possible to connect to a TCP port when the server that is creating the bind is not running. are you able to connect with for example telnet / nc to the not running server?

tobiges commented 3 years ago

yes, I'm able to connect to the port with telnet. I think this might be a WSL bug, but I didn't find any open issues about this behavior yet.