【发布时间】:2011-10-12 05:49:35
【问题描述】:
我正在开发使用 node.js 作为服务器的长轮询应用程序。具体任务是立即学习,然后用户下线。所以,我必须监听 request.close 事件,对吧?
我从How to check if connection was aborted in node.js server复制了以下代码
var http = require("http"),
util = require("util");
var httpServer = http.createServer(function(req, res) {
util.log("new request...");
// notify me when client connection is lost
req.on("close", function(err) {
util.log("request closed...");
});
// wait with response for 15 seconds
setTimeout(function() {
res.writeHead(200, {'Content-Type': 'text/plain'});
res.write("response");
res.end();
util.log("response sent...");
}, 15000);
});
httpServer.listen(8888, "127.0.0.1");
util.log("Running on 8888");
我无法让它正常工作:
我在浏览器中打开与 node.js 服务器连接的特定 URL
然后立即单击“停止”按钮。
Node.js 等待 15 秒,就好像连接仍然存在一样, 然后记录“已发送响应”。
我关闭了 nginx 以防止出现任何问题,结果是一样的。我也尝试听 req.connection.on("close") 事件,结果还是一样。 nodejs 版本是 0.4.12。
可能出了什么问题?
【问题讨论】:
标签: http node.js long-polling