每隔3000ms调用一次http请求,用http.Agent来管理keep-alive的socket,然而我发现当我设置keepAliveMsecs不管多小,后面的请求都会共用第一个socket,请问这是怎么回事儿呢?
const http = require(‘http’); const url = require(‘url’);
let options = url.parse(‘http://localhost:9009’); options.headers = { ‘connection’:‘keep-alive’ } options.agent = new http.Agent({ keepAlive: true, keepAliveMsecs: 20 }); console.log(options) let sockets = []; function request(){ http.get(options,(res)=>{ console.log(res.headers); res.pipe(process.stdout); }).on(‘socket’,socket=>{ if(sockets.indexOf(socket) === -1){ console.log(‘new socket created’); sockets.push(socket); } else { console.log(‘share the socket!’); } }) }
setInterval(request,3000)
server 的keepAliveTimeoutt默认5s有没有改
@Yuki-Minakami 但是客户端可以自己决定什么时候断开啊
@laoqiren 说得也是,我回去试试
@Yuki-Minakami 另外就算我每隔5s+发一个请求,同样也是这种情况。