client and server communication, client network outage, power outage, why the server can not independently detect client disconnection (without heartbeat mechanism).
client and server communication, client network outage, power outage, why the server can not independently detect client disconnection (without heartbeat mechanism).
normal disconnection client will send a fin packet to the server. The server will not know the connection is broken until it receives the fin packet.
when the network is cut off and the power is cut off, the client cannot send fin packets to the server, so the server cannot detect that the client is short-term.
in order to alleviate this problem, the server needs to have a heartbeat logic, that is, when the server detects how long a client has not sent any data, it thinks that the client has been disconnected.
this requires the client to send heartbeat data to the server regularly to maintain the connection
. However, it is not necessary to be so complicated in the implementation process. After linking to websocket, To maintain a link, you only need to initiate a heartbeat to the client every few seconds, and if you fail to send it three times in a row, you can disconnect it.
in implementation, it is best not to let the client send the heartbeat all the time, so that if the client does not write well, it is easy to have problems.