Last active
October 9, 2015 14:18
-
-
Save gquental/3521762 to your computer and use it in GitHub Desktop.
CW35 Hack Thursday - NodeJS - Sockets
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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