节点http.request,如果在发送请求后Internet停止,则不会触发任何事件 [英] Node http.request, no event(s) triggered if Internet stopped after sending the request

查看:176
本文介绍了节点http.request,如果在发送请求后Internet停止,则不会触发任何事件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在服务器上有以下PHP脚本,将等待10秒钟并说出Hello:

I have the following PHP Script on server that will wait 10 seconds and say Hello:

<php sleep(10); ?>Hello

在客户端(节点)上,我有以下内容:

On the client side (node), I have the following:

var http = require('http');
http.get ('http://example.com/sleep.php', function (resp) {
    resp.on('data', function (d) {
        console.log ('data!', d.toString());
    });
    resp.on('end', function (d) {
        console.log ('Finished!');
    });
}).on('error', function (e) {
    console.log ('error:', e);
});

问题是,如果在请求期间互联网连接停止,它将不会触发errorend事件.

The problem is, if the internet connection stopped during the request, it will not trigger error OR end events.

要重现该问题:

  • 将PHP脚本放置在Internet上的某个地方
  • 执行节点脚本
  • 断开互联网连接
  • 脚本什么都不做

我还发现,如果连接在10秒内恢复正常,它仍然可以接收到消息.

I've also found that if the connection is back within 10 seconds, it can still receive the message.

因此,我做了一个简单的间隔循环来检查状态.但是,它无法检测到连接是否已停止工作或仍在等待响应:

So, I made a simple interval loop to check the status. However it can't detect if the connection has stopped working or still waiting for response:

var http = require('http');
var lastRespond = 0, intervalCheck;
var respFinished = false;
http.get ('http://jixun.no-ip.org/sleep.php', function (resp) {
    resp.on('data', function (d) {
        lastRespond = new Date;
        console.log ('data!', d.toString());
    });
    resp.on('end', function (d) {
        respFinished = true;
        console.log ('Finished!');
    });
}).on('error', function (e) {
    console.log ('error:', e);
});
intervalCheck = setInterval(function () {
    if (respFinished) {
        clearInterval(intervalCheck);
    } else if (new Date - lastRespond >= 120000) {
        console.log ('Timeout   :(');
        clearInterval(intervalCheck);
    }
}, 120000); // 2 mins.

所以我的问题是:有什么方法可以在发送请求后检查套接字是否关闭/连接是否停止? 预先感谢.

So my question is: Is there any way to check if the socket closed / connection stopped after sending the request? Thanks in advance.

推荐答案

在实际请求上使用setTimeout可以解决您的问题.嗯,这不是像关闭",结束"和错误"这样的实际事件.下面的示例确实重现并解决了该问题,尽管没有在其他上下文中尝试过.

Using setTimeout on the actual request could solve your problem. Well, it's not an actual event like 'close', 'end' and 'error'. Sample below does reproduce and solves the issue, haven't tried it in another context though.

var http = require('http'); 
http.get ('http://fake-response.appspot.com?sleep=5', function (resp) { 
  resp.on('data', function (d) {
    console.log ('data!', d.toString()); 
  }); 
  resp.on('end', function (d) { 
     console.log ('Finished!'); 
  }); 
}).on('error', function (e) {
    console.log ('error:', e); 
}).setTimeout(12000, function ( ) { 
    console.log('Timeout reached...'); 
    process.exit(1);
});        

更多信息,请参见文档.要么使用它,要么也监听关闭"事件,这与net模块很好地配合.

More information can be found in the documentation. Either use that or listening on the 'close' event as well, that works well with the net module.

这篇关于节点http.request,如果在发送请求后Internet停止,则不会触发任何事件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆