Skip to content

Instantly share code, notes, and snippets.

@mythosil
Created October 18, 2011 04:19
Show Gist options
  • Save mythosil/1294603 to your computer and use it in GitHub Desktop.
Save mythosil/1294603 to your computer and use it in GitHub Desktop.
tcp chat server (nodejs)
var net = require('net');
var streams = new Array();
var server = net.createServer(function(stream) {
stream.setEncoding('utf8');
stream.on('connect', function() {
stream.write('Welcome\r\n');
streams.unshift(stream);
});
stream.on('data', function(data) {
for (i = 0; i < streams.length; i++) {
if (streams[i] !== stream) {
streams[i].write(data);
}
}
});
stream.on('end', function() {
for (i = 0; i < streams.length; i++) {
if (streams[i] === stream) {
streams.splice(i, 1);
break;
}
}
stream.destroy();
});
});
server.listen(1986, '127.0.0.1');
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment