Keyboard shortcuts

Press or to navigate between chapters

Press S or / to search in the book

Press ? to show this help

Press Esc to hide this help

Chat server

var net = require('net');

var sockets = [];

var s = net.Server(function(socket) {
    sockets.push(socket);
    socket.on('data', function(txt) {
        for (var i=0; i < sockets.length; i++) {
            sockets[i].write(txt);
        }
    });
});

s.listen(8000);

This works, but when someone disconnects, the dead socket remains in the array and when we try to write to it it will blow up.