websocket客户端关闭在开关网络/离线时失败
我想问一些有关如何在离线/交换网络时关闭WebSocket客户端的问题。
当我尝试关闭Chrome中2个情况的套接字时,我致电WebSocket.Close.Close。我无法长时间(大约60年代)收到Onclose事件,然后我可以最终收到它。
检查Readystate后,我发现在即将到来的60年代,状态为2(闭幕),未转为3(封闭)。
因此,我想知道,在离线/交换网络条件下调用WebSocket.Close()时,我错过了任何步骤。当网络正常时运行良好。
i'd like to ask some question about how to close a websocket client when offline/switched network.
when i try to close the socket for the 2 case in chrome, after i call websocket.close, i cannot recieve onclose event for a long time (around 60s), then i can recieve it finally.
after i check the readystate, i found that in the coming 60s, the state is 2(CLOSEING), not turned to 3(CLOSED).
so i'd like to know is there any steps i missed when i call websocket.close() in offline/switched network condition. while it runs well when the network is normal.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您的后端框架是什么?
如果您尝试处理突然脱机的客户端网络,则有两种方法可以尝试从客户端关闭Websocket。
请参考源代码在这里。
使用JS离线事件句柄
如果我们想检测用户是否脱机,我们只需在离线事件功能中添加Websocket CLOSE功能。
前端
后端(WebSocketServer)
使用客户端上的ping间隔,并减少服务器端上的WebSocket超时
如果WebSocket服务器在特定时间内未接收任何消息,则将导致超时。因此,如果客户未因离线而发送任何ping,我们可以使用此机制减少超时会话。
。
前端
后端(WebSocketConfig)
what's your back-end framework?
If you try to handle client's network that suddenly turned offline, there're two way you can try to close websocket from client as follows.
Kindly refer to source code here.
Using the js offline event handle
If we would like to detect if the user went offline we simply add websocket close function into offline event function.
front-end
back-end (WebSocketServer)
Using Ping interval on the client-side and decrease the websocket timeout on server-side
If websocket server doesn't receive any message in specific time, it will lead to a timeout. So we can use this mechanism to decrease the timeout to close session if the client doesn't send any ping due to offline.
front-end
back-end (WebSocketConfig)