Created
January 17, 2011 20:46
-
-
Save muloka/783449 to your computer and use it in GitHub Desktop.
Taken from Introduction to Node: Ryan Dahl
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
// source: http://camp.nodejs.org/videos/session-01_introduction_to_node-ryan_dahl.html | |
// Handles TCP connections | |
net = require('net'); | |
// define global variables | |
people = [] | |
// create tcp server | |
s = net.createServer(function(socket) { | |
// push socket connections into people array | |
people.push(socket); | |
// when socket receives data | |
socket.on("data", function(data) { | |
// loop through all open sockets | |
for (var i=0; i < people.length; i++) { | |
// write data to socket other than your own | |
if (people[i] != socket) { | |
people[i].write(data); | |
} | |
} | |
}); | |
// when socket closes | |
socket.on("end", function() { | |
// remove person from socket pool | |
var i = people.indexOf(socket); | |
people.splice(i, 1); | |
}); | |
}); | |
// bind tcp server to port | |
s.listen(8000); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment