Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>The trick is that you need to subscribe to the server's <code>connection</code> event which gives you the socket of the new connection. You need to remember this socket and later on, directly after having called <code>server.close()</code>, destroy that socket using <code>socket.destroy()</code>.</p> <p>Additionally, you need to listen to the socket's <code>close</code> event to remove it from the array if it leaves naturally because its keep-alive timeout does run out.</p> <p>I have written a small sample application you can use to demonstrate this behavior:</p> <pre><code>// Create a new server on port 4000 var http = require('http'); var server = http.createServer(function (req, res) { res.end('Hello world!'); }).listen(4000); // Maintain a hash of all connected sockets var sockets = {}, nextSocketId = 0; server.on('connection', function (socket) { // Add a newly connected socket var socketId = nextSocketId++; sockets[socketId] = socket; console.log('socket', socketId, 'opened'); // Remove the socket when it closes socket.on('close', function () { console.log('socket', socketId, 'closed'); delete sockets[socketId]; }); // Extend socket lifetime for demo purposes socket.setTimeout(4000); }); // Count down from 10 seconds (function countDown (counter) { console.log(counter); if (counter &gt; 0) return setTimeout(countDown, 1000, counter - 1); // Close the server server.close(function () { console.log('Server closed!'); }); // Destroy all open sockets for (var socketId in sockets) { console.log('socket', socketId, 'destroyed'); sockets[socketId].destroy(); } })(10); </code></pre> <p>Basically, what it does is to start a new HTTP server, count from 10 to 0, and close the server after 10 seconds. If no connection has been established, the server shuts down immediately.</p> <p>If a connection has been established and it is still open, it is destroyed. If it had already died naturally, only a message is printed out at that point in time.</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload