Skip to content

Instantly share code, notes, and snippets.

@halldorel
Created June 24, 2014 23:46
Show Gist options
  • Save halldorel/927e081afdb4799f96a2 to your computer and use it in GitHub Desktop.
Save halldorel/927e081afdb4799f96a2 to your computer and use it in GitHub Desktop.
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