首页 > 解决方案 > 有没有办法让 HTTP 请求在准确的时间到达服务器?

问题描述

我正在创建一个发送 HTTP 请求的 nodejs 程序。我希望 HTTP 请求在准确的时间到达服务器。例如,我可能想发送一个 HTTP 请求集,并让它在 2019 年 8 月 6 日星期二下午 1:00:00 或 1565096400000(以毫秒为单位的纪元时间戳)准确地到达服务器。我该怎么做?

我考虑过的一件事是使用 setTimeout() 在某个时间为我的请求计时,但它们到达服务器的时间不一致。

这是一个使用 setTimeout 为我的请求计时的示例。在此示例中,我希望请求恰好在 1565096400000(纪元时间戳)到达服务器。


var options = {
    url = 'someurl.com';
}

function callback(error, response, body) {
    if(!error) {
        console.log('Error.');
    } else {
        console.log(body);
    }
}

var time_until_request = 1565096400000 - Date.now(); 
//We want the request to reach the server at exactly 1565096400000 (epoch //timestamp).

var timeout = setTimeout(function(){
    request(options, callback);
},time_until_request);

当然,就我所做的而言,请求不会在我想要的时候到达服务器。它将在之前或之后的某个时间到达服务器 - 我相信由于延迟不一致。

标签: httprequesttiming

解决方案


推荐阅读