September 2014
Intermediate to advanced
316 pages
7h 6m
English
We will use our current app as the basis since most of it can be reused. Copy namespaces.js and namespace.html and create rooms.js and rooms.html. Open up rooms.js and get rid of the namespace connection listener, as we are only using rooms here. Then, we will modify the normal connection and add our room-specific elements to it. Your rooms.js should look like the following code:
var io = require('socket.io').listen(4000); io.sockets.on('connection', function(socket){ socket.on('join', function(data){ socket.username = data.username; socket.join(data.room); socket.broadcast.to(data.room).emit('join', {username: data.username, socket: socket.id, room: data.room}); }); socket.on('ping', function(data){ socket.broadcast.to(data.room).emit('ping', ...