Skip to content

Make p2pSocket (socket, next, room) take room as string #4

@phonon

Description

@phonon

I initially had trouble getting peers to only connect within some socket room using the function p2pSocket (socket, next, room). The source of the confusion was on the server side in the socket.io-p2p-server/index.js code:

function p2pSocket (socket, next, room) {
  clients[socket.id] = socket
  if (typeof room === 'object') {
    var connectedClients = socket.adapter.rooms[room.name]
  } else {
    var connectedClients = clients
  }
//...

where this requires that the input room be an object. I was passing in room as a string, so this socket.io-p2p-server was connecting me to all clients rather than clients within the room.

Is there any reason that p2pSocket (socket, next, room) requires room to be an object? Based on the code, room is only used to access the room.name string to find the clients in that socket room in that first block cited above. Why not allow clients to pass in room as a string. I think this would reduce some confusion about using p2pSocket. Or maybe even something like:

  if (typeof room === 'object') {
    // ...
  } else if (typeof room === 'string') {
    // ...
  } else {
    // ...
  }

In the mean time, as a hack I have been invoking this function p2pSocket like:

// ...
var p2p = require('socket.io-p2p-server').Server;
// ...
p2p(socket, null, {name: room});

so that the room input is recognized as an object.

Thanks!

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions