http - 如何检测用户取消请求

标签 http proxy node.js

我正在通过编写一个非常基本的 http/web 缓存代理来试用 Node.js,但遇到了一些我未能突破的问题。

假设我有一个非常基本的代理功能(监听请求,将其通过管道传输到外部服务器,等待响应,通过管道返回给客户端),我如何检测客户端(网络浏览器)何时取消请求?当用户在他的浏览器上单击“停止”/Esc 时,浏览器不会向我发送任何“请求”或信息,也不会在“响应”连接结束时附加回调。

我的意思是:

http.createServer (clientRequest, clientResponse) {  
    var client = http.createClient (port, hostname);
    var request = client.request (method, url, headers);  

    request.addListener ('response', function(response){  
        response.addListener ('data', function(chunk){  
           // forward data to clientResponse..
        }  
        response.addListener ('end', function(){   
           // end clientResponse..  
        }  
    });  
    clientResponse.addListener('end', function(){  
        // this never gets called :(
        // I want it to terminate the request/response created just above  
    }
}

最佳答案

原来我应该绑定(bind)到请求的“关闭”事件而不是“结束”事件。 这确实有道理。
我在这里发布这个给可能遇到同样问题的任何人:

clientResponse.addListener('close', function(){  
    // this gets called when the user terminates his request  
}

关于http - 如何检测用户取消请求,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5774093/

相关文章:

javascript - 以与 jQuery.proxy() 相同的方式调用 lodash _.bind() ?

java - 任何方式来监控 android 上应用程序的所有 GET 请求

node.js - 使用docker-compose的docker容器中的 Node 检查器

linux - iptables:只允许 OUTPUT 用于 http 和 ssh

.net - 如何使用 WebRequest 仅下载 header

node.js - 错误! Windows_NT 6.3.9600,windows8无法安装cordova

node.js - heroku bash 无法识别 sequelize 命令

javascript - React Hooks - 使用 useState 与仅使用变量

http - 通过 http 发送 jpg 的问题 - node.js

c++ - http C++ 库?