Thursday 1 June 2017

Socket.io connection joining the wrong room

When a request is made to localhost:3000/:id I want to create a room and add the client to it. Then I want the actions taken by that client to only be broadcasted to the other clients in that room. here's the server side code that handles that:

app.get('/:id', function response(req, res) {
    io.on('connection', function (socket) {

            socket.join('/game-' + req.params.id);

            socket.on('action', function (action) {
                switch (action.type) {
                    case 'TOGGLE_ACTIVE_PLAYER':
                        return socket.broadcast.to('/game-' + req.params.id).emit('action', toggleActivePlayer())
                    case 'SET_SLOT_PREVIEW':
                        return socket.broadcast.to('/game-' + req.params.id).emit('action', setSlotPreview(action.colIndex))
                    case 'CLEAR_SLOT_PREVIEW':
                        return socket.broadcast.to('/game-' + req.params.id).emit('action', clearSlotPreview(action.colIndex))
                    case 'PLAYER_MOVED':
                        return socket.broadcast.to('/game-' + req.params.id).emit('action', playerMoved(action.colIndex))
                    case 'CHECK_FOR_WINNER':
                        return socket.broadcast.to('/game-' + req.params.id).emit('action', checkForWinner())
                    case 'CLEAR_BOARD':
                        return socket.broadcast.to('/game-' + req.params.id).emit('action', clearBoard())
                    case 'CLEAR_WINNER':
                        return socket.broadcast.to('/game-' + req.params.id).emit('action', clearWinner())
                    case 'RESET_GAME':
                        return socket.broadcast.to('/game-' + req.params.id).emit('action', resetGame())
                }
            })
    });
    res.end();
});

The problem I'm having is that when a connection is made with a new id param (localhost:300/newIdParam) a room called newIdParam gets created and that client gets added to it BUT that client also gets inappropriately added to an existing room. I can tell the client is in both rooms by inspecting the io.sockets.adapter.rooms object:

  '/game-123': 
   Room {
     sockets: 
      { '/#KIq0i-_oaf_mtM8AAAAK': true,
        '/#Sokr_2ROg0culZ6bAAAL': true,
        '/#w6XRgbfF3Q2TIicXAAAM': true },
     length: 3 },
  '/#Sokr_2ROg0culZ6bAAAL': Room { sockets: { '/#Sokr_2ROg0culZ6bAAAL': true }, length: 1 },
  '/#w6XRgbfF3Q2TIicXAAAM': Room { sockets: { '/#w6XRgbfF3Q2TIicXAAAM': true }, length: 1 },
  '/game-456': Room { sockets: { '/#w6XRgbfF3Q2TIicXAAAM': true }, length: 1 } }

Notice socket id #w6XRgbfF3Q2TIicXAAAM exists in both rooms. Again these rooms are created simply by making a request to with the id as a route parameter like this:

localhost:3000/123

That request would create a room called game-123 and the requesting client automatically gets added.

Thanks in advance for any ideas.



via macksol

No comments:

Post a Comment