Socket.IO:断开连接时的XHR轮询延迟 [英] Socket.IO: XHR-Polling delay on disconnection

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

问题描述

我正在使用socket.io和node.js.由于套接字IO识别XHR-Polling客户端已断开连接,因此延迟了几秒钟,因此我无法跟踪哪些用户处于在线状态.

如果XHR-Polling客户端刷新页面,则其新连接消息似乎在其断开连接消息之前.尝试跟踪哪些用户在线时,这会引起混乱.

If an XHR-Polling client refreshes the page their new connection message seems to precede their disconnection message. This causes confusion when trying to track which users are online.

一种解决方案可能是检测服务器端的传输方法并延迟XHR-Polling客户端的连接,以确保断开连接功能已首先运行.

One solution could be to detect the transport method on the serverside and delay the connection for XHR-Polling clients to ensure that the disconnection functions have been run first.

有人对此有任何经验吗?

推荐答案

注释中存在的主要问题(如@davin和@jcolebrand指出)是不能仅依靠连接.用户离开页面后,网络设备可以保持连接.因此,您需要采取其他措施来确认用户的存在.

The main problem with presence, as @davin and @jcolebrand point out in the comments, is that connections alone can't be relied upon. Network devices can hold on to connections after a user has navigated away from a page. So, you need something else in place to confirm a users presence.

这是我们在 Pusher 中添加的支持,我们使用称为

This is something we've added support for in Pusher and we do this using something called Presence Channels.

我们使用唯一的用户ID(您(开发人员)在用户连接时必须提供)以及标识连接的唯一套接字ID来管理此问题.由于此唯一用户ID在状态成员列表中只能出现一次,因此,即使该用户打开了多个连接,因此也打开了多个套接字连接,该用户也只会被列出一次.

We manage this using a unique user id, which you (the developer) have to supply when a user connects, in combination with a unique socket id which identifies the connection. Since this unique user id can only occur once within a presence member list it means that that user will only be listed once - even if they have multiple connections open, and thus multiple socket connections.

相同的想法可以应用于HTTP连接.

The same idea can be applied to HTTP connections.

通常的方法是使用会话来唯一地标识用户.这样,即使它们只是来宾",只要它们允许使用cookie,您仍然可以识别它们.

The general approach for this is to use the session to uniquely identify the user. In that way even if they are just a "Guest" you can still identify them as long as they are allowing the use of cookies.

因此,系统上的用户数应永远不超过服务器上正在运行的活动会话数.

So, the number of users on your system should only ever max at the number of active sessions you have running on your server.

希望这会有所帮助.让我知道您是否想澄清什么.

Hope this helps. Let me know if you'd like anything clarified.

这篇关于Socket.IO:断开连接时的XHR轮询延迟的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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