简体   繁体   English

注意传入的 HTTP 请求的连接终止 - Node.JS

[英]Watch for connection termination on incoming HTTP requests - Node.JS

When making requests in Node.JS to another HTTP server, you can listen for when the server closes the connection with request.on('close', function(){});在 Node.JS 中向另一个 HTTP 服务器发出请求时,您可以使用request.on('close', function(){}); . . I've been searching for similar functionality within Node's http.createServer() .我一直在 Node 的http.createServer()中寻找类似的功能。 I assumed that that request variable passed in to the callback had an on() function which I could call to listen for when the client closes the connection.我假设传递给回调的请求变量有一个on() function,当客户端关闭连接时我可以调用它来监听。

var http = require('http');
http.createServer(function(req, res) {
    req.on('close', function() { console.log("Connection closed"); });
}).listen(80, '127.0.0.1');

However, after reading the 0.4.9 docs on streams , I noticed that under the close event it said:但是,在阅读了有关流的 0.4.9 文档后,我注意到在 close 事件下它说:

Not all streams will emit this.并非所有的流都会发出这个。 (For example, an incoming HTTP request will not emit 'close'.) (例如,传入的 HTTP 请求不会发出“关闭”。)

Is there a way to listen for client connection terminations from the server in Node?有没有办法在 Node 中侦听来自服务器的客户端连接终止?

Well, you could access the socket directly by doing:好吧,您可以通过执行以下操作直接访问套接字:

req.socket.on('close', ...);

However, doing this inside a request listener will bind the event for every request.但是,在请求侦听器中执行此操作将为每个请求绑定事件。 So instead you should bind to the connection event:因此,您应该绑定到连接事件:

var server = http.createServer();
server.on('request', function(req, res) { ... });
server.on('connection', function(socket) {
  socket.on('close', function() {
    console.log(socket.remoteAddress + '\'s keep-alive session died!');
  });
});

EDIT: Maybe I should have mentioned, yes, technically the socket object for an http server will carry a reference to the response on the _httpMessage property.编辑:也许我应该提到,是的,从技术上讲,用于 http 服务器的套接字 object 将引用_httpMessage属性上的响应。 However, I would advise against using this as it is part of the internal API and is subject to change without any warning.但是,我建议不要使用它,因为它是内部 API 的一部分,并且可能会在没有任何警告的情况下进行更改。

It would be better to add your own property to get a reference to the last request made:最好添加您自己的属性以获取对上次请求的引用:

server.on('request', function(req, res) {
  req.socket.currentRequest = req;
});
server.on('connection', function(socket) {
  socket.on('close', function() {
    var req = socket.currentRequest;
    console.log('Socket closed. Last request for: ' + req.url);
  });
});

Socket.io Socket.io

When you want to know if a socket has closed, which only makes sense when you are do a hanging requests(real-time) I advice you to use socket.io instead, which takes care of all this:当您想知道套接字是否已关闭时,这仅在您执行挂起请求(实时)时才有意义,我建议您改用socket.io ,它会处理所有这些:

var io = require('socket.io').listen(80);

io.sockets.on('connection', function (socket) {
  io.sockets.emit('this', { will: 'be received by everyone');

  socket.on('private message', function (from, msg) {
    console.log('I received a private message by ', from, ' saying ', msg);
  });

  socket.on('disconnect', function () {
    sockets.emit('user disconnected');
  });
});

The nice thing about socket.io is that it's support by every browser(major) without you having to worry about the little details(which make you go insane). socket.io 的好处是它得到了每个浏览器(主要)的支持,而您不必担心小细节(这会让您发疯)。 They have abstracted that with a nice API.他们用一个不错的 API 抽象了这一点。

Express :快递

Or when using Express或者使用 Express 时

var app = require('express').createServer();

app.get('/', function(req, res){
    req.on('close', function () {
        console.log('closed');
    });
});

app.listen(3000, '127.0.0.1');

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM