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.

Revisions

  1. csabapalfi created this gist Oct 3, 2018.
    31 changes: 31 additions & 0 deletions keep-alive.js
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,31 @@
    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();
    });
    });