request.close 事件不会在 Node.js 服务器中触发

发布于 2024-12-09 09:26:35 字数 1127 浏览 0 评论 0原文

我正在开发使用 node.js 作为服务器的长轮询应用程序。特定的任务是立即了解用户离线情况。所以,我必须监听 request.close 事件,对吗?

我从 如何检查连接是否在node.js服务器中中止

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");

我无法使其正常工作:

  1. 我在浏览器中打开与node.js服务器连接的特定URL

  2. 之后立即单击“停止”按钮。

  3. Node.js 等待 15 秒,就好像连接仍然存在一样, 然后记录“响应已发送”。

我关闭了 nginx 以防止出现任何问题,结果是一样的。我也尝试监听 req.connection.on("close") 事件,结果又是一样的。 Nodejs版本是0.4.12。

可能出什么问题了?

I'm working on the long-poll application which uses node.js as a server. The particular task is to immediately learn then the user goes offline. So, I have to listen for the request.close event, right?

I copied following piece of code from the 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");

I can't make it working properly:

  1. I open in the browser specific URL connected with the node.js server

  2. Immediately after that click the "Stop" button.

  3. Node.js waits for 15 seconds as if the connection was still alive,
    and then logs "response sent".

I turned off nginx to prevent any problems, the result is the same. I tried to listen the req.connection.on("close") event also, the result is the same again. The nodejs version is 0.4.12.

What could be wrong?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

私野 2024-12-16 09:26:35

首先,您两次提到了 onclose 事件,然后发布了侦听 close 事件的代码。有些人看到类似的东西时会跳过你的问题。

其次,TCP 连接是操作系统代表应用程序管理的东西。根据建立和断开连接的方式,TCP 连接完全有可能在您认为已断开连接后仍停留几分钟。对TCP、socket和linger进行一些研究。

First of all, you twice mentioned an onclose event and then posted code that listens for a close event. Some people will just skip your question when they see stuff like that.

Secondly, TCP connections are something that the OS manages on behalf of applications. Depending on how you make and break a connection, it is entirely possible for a TCP connection to linger for several minutes after you think that you broke it. Do some research on TCP, socket and linger.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文