javascript node.js http 服务器,检测客户端何时断开连接
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/6572572/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
node.js http server, detect when clients disconnect
提问by Jeroen
I am using express with node.js for a http server. I store the response object so I can stream events down to the client on that channel. Is there a way to detect when the client disconnects? When I kill my client I can still write to the response object without getting any kind of exception/error. It looks like the service keeps the underlying tcp channel open as long as I keep writing to the response object.
我正在将 express 与 node.js 一起用于 http 服务器。我存储响应对象,以便我可以将事件向下传输到该通道上的客户端。有没有办法检测客户端何时断开连接?当我杀死我的客户端时,我仍然可以写入响应对象而不会出现任何异常/错误。看起来只要我继续写入响应对象,服务就会保持底层 tcp 通道打开。
回答by Raynos
req.on("close", function() {
// request closed unexpectedly
});
req.on("end", function() {
// request ended normally
});