Created
October 3, 2018 14:00
-
-
Save csabapalfi/e6a77c9c2a8b1cb474503ada5c373645 to your computer and use it in GitHub Desktop.
node keep-alive
This file contains 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 net = require('net'); | |
const {parse} = require('url'); | |
const [,,url] = process.argv; | |
const client = new net.Socket(); | |
const {hostname, port = 80, path} = parse(url); | |
client.on('error', (e) => {throw e}); | |
const request = (cb) => { | |
console.log(`GET ${path} ...`); | |
client.write(`GET ${path}\r\nConnection: keep-alive\r\n\r\n`); | |
client.once('data', (data) => { | |
const [status,] = data.toString().split('\n'); | |
console.log(status); | |
cb(); | |
}); | |
}; | |
const requests = (delay, cb) => | |
request(() => setTimeout(() => request(cb), delay)); | |
client.connect(port, hostname, (err) => { | |
console.log(`Connected to ${hostname},${port}...`); | |
if (err) { | |
throw err; | |
} | |
requests(6000, () => { | |
client.destroy(); | |
}); | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment