socket.io 通过 XHR 轮询强制断开连接 [英] socket.io force a disconnect over XHR-polling

查看:27
本文介绍了socket.io 通过 XHR 轮询强制断开连接的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个客户端/服务器应用程序,在服务器上使用 nodejs,并将 socket.io 作为连接机制.由于与我的应用程序相关的原因,我希望每个浏览器只有一个活动连接,并拒绝来自其他选项卡的所有连接,这些连接可能在会话期间稍后打开.这适用于 WebSockets,但如果浏览器不支持 WebSockets 而使用 XHR 轮询,则断开连接永远不会发生,因此如果用户只是刷新页面,这不会被解释为重新连接(我有延迟重新连接和会话恢复),但作为一个新选项卡,最终连接被拒绝,因为由该选项卡建立的旧连接仍然处于活动状态.

I have a client/server application using nodejs on the server and socket.io as the connection mechanism. For reasons relevant to my application I want to have only one active connection per browser, and reject all the connections from other tabs that may be opened later on during the session. This works great with WebSockets, but if WebSockets is not supported by the browser and XHR-polling is used instead, the disconnection never happens, so if the user just refreshes the page, this is not interpreted as a reconnection ( I have a delay for reconnection and session restoring), but as a new tab, which ends in the connection being rejected because the old connection made by this same tab is still active.

我正在寻找一种方法来在发生刷新时有效地结束来自客户端的连接.我已经尝试绑定到 beforeunload 并在客户端调用 socket.disconnect(),并发送类似 socket.emit('force-disconnect') 并触发与服务器的断开连接但没有成功.我在这里错过了什么吗?感谢你的帮助!

I'm looking for a way to effectively end the connection from the client whenever a refresh occurs. I've tried binding to the beforeunload and calling socket.disconnect() on the client side, and also sending a message like socket.emit('force-disconnect') and triggering the disconnect from the server with no success. Am I missing something here? Appreciate your help!

我已经阅读了这个问题 并没有发现它对我的特殊情况有用.

I've read this question and couldn't find it useful for my particular case.

推荐答案

解决了问题,原来是bugsocket.io 0.9.5 中引入.如果您遇到此问题,只需将您的服务器和客户端代码都更新为 socket.io >0.9.9 并将 socket.io 客户端选项 sync disconnect on unload 设置为 true 就可以了.

Solved the issue, it turns out it was a bug introduced in socket.io 0.9.5. If you have this issue just update BOTH your server and client-side code to socket.io > 0.9.9 and set the socket.io client-side options sync disconnect on unload to true and you're all set.

选项是这样设置的:

var socket = io.connect('http://yourdomain.com', {'sync disconnect on unload' : true});

这篇关于socket.io 通过 XHR 轮询强制断开连接的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆