我可以在socket.io中的命名空间中使用房间吗?

时间:2022-08-22 15:47:44

I'd like to know whether I can use multiple rooms withing a namespace in socket.io.

我想知道我是否可以在socket.io中使用带有命名空间的多个房间。

As far as I know you can use either namespaces OR rooms.

据我所知,您可以使用任一命名空间或房间。

My purpose is to have multiple unique instances of my application talking with other ones with this unique application through websockets with just one node.js-socket.io-server.

我的目的是让我的应用程序的多个唯一实例通过只有一个node.js-socket.io-server的websockets与这个独特的应用程序交谈。

Withing this application there are requirements to talk to each other. Either within a room or global. There is no need to talk to another namespace.

有了这个应用程序,就有了相互交流的要求。在房间内或全球范围内。无需与另一个命名空间通信。

Thanks in advance,

提前致谢,

Daniel

1 个解决方案

#1


I have had no problem doing this. I have a few multiplayer games that I built (see here), and they all run on the same socket.io instance. The namespacing keeps the different games separated, and the rooms keep individual game rooms within a single game separated.

我这样做没有问题。我有一些我构建的多人游戏(见这里),它们都运行在同一个socket.io实例上。命名空间使不同的游戏分开,并且房间将单个游戏室分开。

Full code example here: https://github.com/azurelogic/CakeRush/blob/master/controllers/cakerush.js

完整的代码示例:https://github.com/azurelogic/CakeRush/blob/master/controllers/cakerush.js

The highlights are the fact that I use namespacing on connection:

重点是我在连接上使用命名空间:

io.of('/sockets/cakerush').on('connection', function (socket) { //...

Then later, I allow users to join a room through a 'join room' event:

然后,我允许用户通过“加入房间”活动加入一个房间:

socket.on('joinRoom', function (data) {
  // find the room being requested
  var room = _.find(rooms, {id: data.roomId});

  /* skip some logic here... */

  // register player with room
  room.playerIds.push(socket.id);
  socket.join(room.id);  
  // send verification that room was joined to the player with room id
  socket.emit('roomJoined', {roomId: room.id, shouldGenerateFirstCake: shouldGenerateFirstCake});
});

#1


I have had no problem doing this. I have a few multiplayer games that I built (see here), and they all run on the same socket.io instance. The namespacing keeps the different games separated, and the rooms keep individual game rooms within a single game separated.

我这样做没有问题。我有一些我构建的多人游戏(见这里),它们都运行在同一个socket.io实例上。命名空间使不同的游戏分开,并且房间将单个游戏室分开。

Full code example here: https://github.com/azurelogic/CakeRush/blob/master/controllers/cakerush.js

完整的代码示例:https://github.com/azurelogic/CakeRush/blob/master/controllers/cakerush.js

The highlights are the fact that I use namespacing on connection:

重点是我在连接上使用命名空间:

io.of('/sockets/cakerush').on('connection', function (socket) { //...

Then later, I allow users to join a room through a 'join room' event:

然后,我允许用户通过“加入房间”活动加入一个房间:

socket.on('joinRoom', function (data) {
  // find the room being requested
  var room = _.find(rooms, {id: data.roomId});

  /* skip some logic here... */

  // register player with room
  room.playerIds.push(socket.id);
  socket.join(room.id);  
  // send verification that room was joined to the player with room id
  socket.emit('roomJoined', {roomId: room.id, shouldGenerateFirstCake: shouldGenerateFirstCake});
});