Last active
April 7, 2019 10:18
-
-
Save sj82516/f5cff0eb62a4fde65cb67c9fbbabfdbb to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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