开发者

How can Node.js detect browser side has ended one HTTP session?

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开发者_StackOverflow中文版 and let us know it is aborted. But, there is no such callback argument.


I think http://socket.io/ will do what you need with client.on('disconnect', function(){ … })


req.socket does not seem to be a published property. I have used:

res.connection.on('close', function() {
...  
});


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..

var server = http.createServer(function(req, res) {
    req.socket.addListener("end", function() {
      ///he gone
    });
});
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜