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

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

问题描述

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

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.

推荐答案

解决了该问题,事实证明这是一个socket.io 0.9.5中引入的rel ="noreferrer">错误.如果遇到此问题,只需将服务器和客户端代码都更新为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天全站免登陆