Skip to content

Instantly share code, notes, and snippets.

@Twipped
Created July 14, 2014 22:25
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save Twipped/09d0e6208c6d5cafc332 to your computer and use it in GitHub Desktop.
Save Twipped/09d0e6208c6d5cafc332 to your computer and use it in GitHub Desktop.
Very simple nodejs chat server
var net = require('net');
var clients = [];
var total = 0;
net.createServer(function (socket) {
clients.push(socket);
var id = clients.length -1;
var name = 'User ' + (++total);
socket._name = name;
clients.forEach(function (client) {
client.write(name+' Connected\n');
});
socket.on('data', function (data) {
clients.forEach(function (client) {
if (client._name !== name) client.write(name+': '+data);
});
});
socket.on('end', function () {
clients.splice(id, 1);
clients.forEach(function (client) {
client.write(name+' Disconnected\n');
});
});
}).listen(6000);
@Twipped
Copy link
Author

Twipped commented Jul 14, 2014

Telnet to port 6000 to connect to the server

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment