Created
November 9, 2015 06:30
-
-
Save nurfarazi/44aa9c82a0d1a71a06f6 to your computer and use it in GitHub Desktop.
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
Array.prototype.remove = function(from, to) { | |
var rest = this.slice((to || from) + 1 || this.length); | |
this.length = from < 0 ? this.length + from : from; | |
return this.push.apply(this, rest); | |
}; | |
// Application | |
var net = require('net'); | |
var sockets = []; | |
net.createServer(function(socket){ | |
sockets.push(socket); | |
socket.write('hello\n'); | |
socket.write('world\n'); | |
socket.on('data', function(data){ | |
var i; | |
for (i=0; i < sockets.length; i++){ | |
if (sockets[i] === socket) continue; | |
sockets[i].write(data.toString()); | |
} | |
}); | |
socket.on('end', function(){ | |
var i = sockets.indexOf(socket); | |
sockets.remove(i); | |
}); | |
}).listen(8000); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment