diff --git a/server.js b/server.js index b0b303e..6a8fe90 100644 --- a/server.js +++ b/server.js @@ -16,6 +16,7 @@ app.io.route('room', { req.socket.emit('room:joined',req.data); req.socket.join(req.data); req.socket.broadcast.to(req.data).emit('message:server', 'A person joined this room'); + req.socket.current_room = req.data; } }, leave: function(req) { @@ -23,6 +24,7 @@ app.io.route('room', { req.socket.emit('room:left'); req.socket.leave(req.data); req.socket.broadcast.to(req.data).emit('message:server', 'A person left this room'); + req.socket.current_room = undefined; } } }); @@ -34,6 +36,16 @@ app.io.route('message', { } }); +app.io.sockets.on('connection', function(socket) { + socket.on('disconnect', function() { + // Notify other users of the room + if( socket.current_room !== undefined ) { + socket.broadcast.to(socket.current_room).emit('message:server', 'A person left this room'); + console.log('Person left' + socket.current_room); + } + }); +}); + app.listen(8080, function(){ console.log('listening on *:8080'); }); \ No newline at end of file