Skip to content

Instantly share code, notes, and snippets.

@gquental
Last active October 9, 2015 14:18
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save gquental/3521762 to your computer and use it in GitHub Desktop.
Save gquental/3521762 to your computer and use it in GitHub Desktop.
CW35 Hack Thursday - NodeJS - Sockets
var net = require('net');
var sockets = [];
function removeSocket(socket) {
var index = sockets.indexOf(socket);
if (index > -1) {
sockets.splice(index, 1);
}
}
net.createServer(function(socket) {
sockets.push(socket);
socket.on('data', function(data) {
sockets.forEach(function(sckt) {
if (sckt != socket) {
sckt.write(data.toString());
}
});
});
socket.on('end', function() {
removeSocket(socket);
});
socket.on('close', function() {
removeSocket(socket);
});
}).listen(3030);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment