我正在创建一个发送HTTP请求的nodejs程序。我希望HTTP请求在准确的时间到达服务器。例如,我可能想发送一个HTTP requeset并将其准确地在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);
当然,根据我正在执行的操作,请求不会在我希望的时间到达服务器。它将在到达服务器之前或之后的某个时间到达服务器-我相信是由于延迟不一致。