Socket IO detect when client has lost connection

前端 未结 2 1404
北恋
北恋 2021-02-19 23:41

I need to be able to detect when a user has lost connection to the socket, it doesn\'t seem that socket.on(\"disconnect\") is being called when I just close my lapt

2条回答
  •  孤街浪徒
    2021-02-20 00:06

    we came across this too and as mentioned above use our own heartbeat timer.

    For example the server will emit a heartbeat every 5 seconds (we simple send out server time). The client has an 11 second timeout so it has 2 chances to catch the heartbeat. If it receives the heartbeat we reset the timer. If we miss both heartbeats the timeout function is run and you do whatever you want there.

    Fairly straightforward.

提交回复
热议问题