0
如果请求的持续时间超过10秒,下面的代码将中止请求。在http请求中设置基于套接字的超时
是否可以使用http
模块在请求上设置基于socket
的超时?
此外,我应该使用套接字和请求超时?
var abortRequestError;
var timeout = 10000;
var requestObject = {
host: 'xx.xx.xx.xx',
path: '/api',
port: 10000,
method: 'GET'
};
var req = http.request(requestObject, function(res) {
var responseBody = '';
res.on('data', function(data) {
responseBody += data;
});
res.on('end', function() {
console.log(abortRequestError);
});
}).on('error', function(error) {
error = abortRequestError ? abortRequestError : error;
console.log(error);
}).setTimeout(timeout, function() {
abortRequestError = new Error('Request timed out');
req.abort();
});
你的意思是连接超时? – elmigranto
只有当数据流中断10秒时才会被认为中断。是否连接超时? – bobbyrne01
您的代码为您想要实现的功能调用正确的函数 - 套接字空闲时超时;也许问题在别处。你想达到什么目标,究竟哪些方法无效? – elmigranto