nodejs websocket detect disconnected socket

前端 未结 4 2224
余生分开走
余生分开走 2021-01-04 20:30

I have a nodejs websocket server and I have the following problem.

When my clients connect to the server and terminate gracefully the onclose method is

4条回答
  •  轻奢々
    轻奢々 (楼主)
    2021-01-04 20:55

    To detect a disconnect, you need to have some traffic. If your application produces constant traffic, then you could do something like reset a counter each time something is received, and consider the connection failed when the counter runs out.

    Otherwise you should be able to use the pings that WebSocket offers. These are not exposed in the browser, but your WebSocket library for Node.js may allow you to turn them on and get a notification if a ping fails.

提交回复
热议问题