Skip to content

Instantly share code, notes, and snippets.

@muloka
Created January 17, 2011 20:46
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 muloka/783449 to your computer and use it in GitHub Desktop.
Save muloka/783449 to your computer and use it in GitHub Desktop.
Taken from Introduction to Node: Ryan Dahl
// 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