Created
July 14, 2014 22:25
-
-
Save Twipped/09d0e6208c6d5cafc332 to your computer and use it in GitHub Desktop.
Very simple nodejs chat server
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
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); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Telnet to port 6000 to connect to the server