我正在使用 Node.js(Express 框架)和 socket.io 做一个实时网站,在某些时候它必须检测是否有人“在线”(基本上是如果有人打开了网页)。
我认为 Facebook 聊天就是一个群组示例。但它背后的工程逻辑是什么?
非常感谢!
我正在使用 Node.js(Express 框架)和 socket.io 做一个实时网站,在某些时候它必须检测是否有人“在线”(基本上是如果有人打开了网页)。
我认为 Facebook 聊天就是一个群组示例。但它背后的工程逻辑是什么?
非常感谢!
这张来自 Facebook 的幻灯片(http://www.erlang-factory.com/upload/presentations/31/EugeneLetuchy-ErlangatFacebook.pdf)详细解释了它的消息系统。系统有comet(channel)集群,保持与浏览器的TCP连接,并定期将连接的用户列表推送到Presence Server。因此,如果用户在线或不在线,网页 (PHP) 可以查询 Presence Server。
Not sure exactly how Facebook does this but if you serve a page to the user that has javascript in it, which makes asynchronous requests at a specific interval say every minute. Then when you receive a request the user is marked online with a timeout slightly greater then the ajax interval. If the time-out expires without another ajax request the user is marked offline.
You could possibly hook into the page load and page close events to only send requests when a user loads the page or leaves it as well. This would require less requests.
EDIT
I see your using socket io I'm not super familiar with this but I imagine you could use the socket connection / disconnection events provided.
客户端定期向服务器发送保持活动消息,在客户端 JS 中实现。
如果服务器在某个超时时间内没有收到keep-alive,它可以将用户标记为离线。当 websocket 显式关闭时,它也可以这样做。