Skip to content

Instantly share code, notes, and snippets.

@afshinm
Created October 20, 2012 18:41
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 afshinm/3924308 to your computer and use it in GitHub Desktop.
Save afshinm/3924308 to your computer and use it in GitHub Desktop.
Simple Chat Server with Socket.IO - Server Code
var buffer = [];
io.sockets.on('connection', function(client) {
client.emit('history', { buffer: buffer });
io.sockets.emit('announcement', client.id + ' connected');
client.on('message', function(message){
var msg = { message: [client.id, message] };
buffer.push(msg);
if (buffer.length > 15) buffer.shift();
io.sockets.emit('message', msg);
});
client.on('disconnect', function(){
io.sockets.emit('announcement', client.id + ' disconnected');
});
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment