Node.js 如何检测浏览器端已结束一个 HTTP 会话?
我正在尝试使用 Node.js 实现 Comet (XHR Streaming)。由于XHR Streaming使客户端XHR.responseText不断增长,因此客户端需要关闭当前XHR并重新启动XHR Streaming。
在服务器端,对于每个 XHR 流,应保留 http.ServerResponse 对象,直到 HTTP 会话结束。
问题是:Node.js 如何检测浏览器端已经结束了一个 HTTP 会话?
我认为,理想情况下,http.ServerResponse.write 有一个回调参数。因此,写入已关闭的 HTTP 会话将触发回调并让我们知道它已中止。但是,没有这样的回调参数。
I'm trying to implement Comet (XHR Streaming) with Node.js. Since, XHR Streaming make client side XHR.responseText keep growing, it is necessary that client side close current XHR and restart XHR Streaming again.
In server side, for each XHR streaming, the http.ServerResponse object should be held until the HTTP session is over.
The problem is: How can Node.js detect browser side has ended one HTTP session?
I thought, ideally, there is one callback argument to http.ServerResponse.write. So that writing to a closed HTTP session would trigger the callback and let us know it is aborted. But, there is no such callback argument.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
我认为 http://socket.io/ 会用
client.on('disconnect' , function(){ … })
I think http://socket.io/ will do what you need with
client.on('disconnect', function(){ … })
req.socket
似乎不是已发布的属性。我用过:req.socket
does not seem to be a published property. I have used:正如 @macarthy 已经提到的:socket.io 是使用 node.js 实现 comet 的方法。
但是,如果您希望自己实现 XHR Streaming,则每个 http.ServerResponse 上应该有一个名为“end”的事件。每个会话都会触发此事件一次,无需任何参数来通知您的应用程序已关闭响应。
As @macarthy already mentioned: socket.io is the way to go for comet-implementations using node.js.
However, if you wish to implement XHR Streaming yourself, there should be an event called 'end' on each http.ServerResponse. This event is triggered once per session without any arguments to notify your application of a closed response.
我总是用类似的东西..
I always use something like..