Skip to content

Instantly share code, notes, and snippets.

@csabapalfi
Created October 3, 2018 14:00
Show Gist options
  • Save csabapalfi/e6a77c9c2a8b1cb474503ada5c373645 to your computer and use it in GitHub Desktop.
Save csabapalfi/e6a77c9c2a8b1cb474503ada5c373645 to your computer and use it in GitHub Desktop.
node keep-alive
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