繁体   English   中英

如何检测用户取消请求

[英]How to detect user cancels request

我正在通过编写一个非常基本的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  
    }
}

原来我应该绑定到请求的“关闭”事件,而不是“结束”事件。 确实有道理。
我在这里发布此消息给其他可能遇到相同问题的人:

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

暂无
暂无

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

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