Reliable WebSocket Connection Status Detection

I look around to implement a reliable WebSocket connection recovery mechanism.

After some investigation, I found that one of the methods sends audible signals to the server (ping / pong) and checks if I receive all the pong for a limited time.

Thus, if the connection is really omitted or it is very slow, it will be considered disconnected if the wait is waiting for the pong to wait, and the code should call WebSocket.close() .

At the end of the day, I ask this question to check the workflow of connecting and reconnecting with WebSockets and to check if I am missing something.

That is, my question is: is this the correct and reliable workflow for implementing the WebSockets reconnection mechanism?

+6
source share
1 answer

The websocket protocol defines special control frames for ping and pong, but they are not available through the JavaScript API. But if the server sends these frames, the browser will respond.

However, if the connection suddenly breaks and becomes a half-open connection , although the server detects it, the browser will not, therefore, I assume that sending your own letters at the application level is a good idea.

Answering your question, yes, this is a good idea, because if the connection becomes half open, your client does not receive updates, because he believes that he is connected. In websocket, the client must initiate the connection, so even if the browser implements disconnection, it cannot do anything to reconnect.

+3
source

Source: https://habr.com/ru/post/971854/


All Articles