Skip to content

Instantly share code, notes, and snippets.

@sj82516
Last active April 7, 2019 10:18
Show Gist options
  • Select an option

  • Save sj82516/f5cff0eb62a4fde65cb67c9fbbabfdbb to your computer and use it in GitHub Desktop.

Select an option

Save sj82516/f5cff0eb62a4fde65cb67c9fbbabfdbb to your computer and use it in GitHub Desktop.
const http = require('http');
const keepAliveAgent = new http.Agent({
keepAlive: true,
// 多久發一次 tcp keep-alive 檢查
keepAliveMsecs: 4000
});
const options = {
hostname: 'localhost',
port: 3000,
path: '/',
method: 'GET',
headers: {
'Connection': 'keep-alive'
},
agent: keepAliveAgent,
};
let req = http.request(options, (res) => {
res.setEncoding('utf8');
res.on('data', (chunk) => {
console.log(`BODY: ${chunk}`);
});
res.on('end', () => {
console.log("end");
setTimeout(() => {
req = http.request(options, (res) => {
res.setEncoding('utf8');
res.on('data', (chunk) => {
console.log(`BODY: ${chunk}`);
});
res.on('end', () => {
console.log('No more data in response.');
});
});
req.end();
}, 6000);
});
});
req.on('error', (e) => {
console.error(`problem with request: ${e.message}`);
});
// write data to request body
req.end();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment