如何检测用户取消请求
我正在通过编写一个非常基本的http / webcaching代理尝试Node.js,并击中了我没有设法突破的东西。
假设我有一个非常基本的代理function(侦听请求,将其传送到外部服务器,等待响应,将其传回客户端),如何检测客户端(Web浏览器)何时取消请求? 当用户在他的浏览器上点击“Stop”/ Esc时,浏览器不会向我发送任何“请求”或信息,并且在“响应”连接结束时没有被调用时附加callback。
这是我的意思:
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 } }
原来,我应该绑定到“closures”事件,而不是请求的“结束”事件。 这确实是有道理的。
我在这里发布这个可能遇到同样问题的其他人:
clientResponse.addListener('close', function(){ // this gets called when the user terminates his request }
你可能想要在这里回答超时吗?