Skip to content

Instantly share code, notes, and snippets.

@sh1mmer
Created September 28, 2011 14:45
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 sh1mmer/1248119 to your computer and use it in GitHub Desktop.
Save sh1mmer/1248119 to your computer and use it in GitHub Desktop.
var net = require('net'),
var clients = []
var server = net.createServer()
server.listen(8000)
server.on('connection', function(connection) {
connection.type = 'tcp'
console.log('I got a connection')
connection.write('Welcome to the chat server\n')
clients.push(connection)
connection.setEncoding('utf8')
connection.on('data', function(data) {
broadcast(data, connection)
})
connection.on('end', function() {
clients.splice(clients.indexOf(connection), 1)
})
})
function broadcast(message, connection) {
for(i=0;i<clients.length;i+=1) {
if(clients[i] !== connection) {
if(clients[i].writable) {
clients[i].write(message)
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment