Created
June 24, 2014 23:46
-
-
Save halldorel/927e081afdb4799f96a2 to your computer and use it in GitHub Desktop.
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 users = []; | |
io.sockets.on('connection', function(socket) | |
{ | |
users.push(socket); | |
socket.on('setUsername', function(data) | |
{ | |
socket.username = data; | |
}); | |
socket.on('sendMessage', function(message) | |
{ | |
var data = {message: message, | |
username: socket.username, | |
datetime: new Date().toISOString()}; | |
socket.broadcast.emit('message', data); | |
console.log(data.username + ": " + data.message); | |
}); | |
socket.on('disconnect', function () { | |
console.log(socket.username, " disconnected"); | |
delete users[users.indexOf(socket)]; | |
}); | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment