Socket.io:命名空间,频道和合作 [英] Socket.io: Namespaces, channels & co

查看:74
本文介绍了Socket.io:命名空间,频道和合作的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个运行顶部套接字服务器的Node.js Web服务器,该服务器使用 Socket.io .基本上,这可行.

I have a Node.js web server that runs a socket server on top, which was created using Socket.io. Basically, this works.

我现在想要实现的是将连接的客户端按组进行群集.因此,可能会有一些客户组成A组,而另一些客户组成B组.他们应通过使用特定的URL(localhost:3000/Alocalhost:3000/B)选择属于哪个组.

What I now want to achieve is that the clients that connect are clustered in groups. So there might be some clients which make up group A and some other clients which make up group B. They shall select to which group they belong by adressing a specific URL, either localhost:3000/A or localhost:3000/B.

在Socket.io中,我现在想向组A中的所有客户端或组B中的所有客户端或所有客户端发送消息,而无需查看他们的组.

In Socket.io I now want to send messages to all clients in group A or to all clients in group B or to all clients, without looking at their group.

基本上就像和房间聊天一样,您可以向特定房间内的所有客户发送消息,也可以向任何客户发送消息,无论他在哪个房间.

It's basically like having a chat with rooms, and you have either messages for all clients within a specific room, or for any client, no matter what room he is in.

使用Socket.io设计此类系统的最佳方法是什么?

What is the best way to design such a system using Socket.io?

到目前为止,我一直在尝试使用命名空间,该命名空间基本上可以用于创建组A和B,但是后来我无法向所有客户端发送消息,无论它们位于什么空间.至少我不知道该怎么做.

I have been trying using namespace so far, which basically works for creating groups A and B, but then I lose the ability to send messages to all clients, no matter what room they are in. At least I don't know how to do this.

我应该如何建模?我应该寻找什么正确的术语?

How should I model this? What are the right terms I should look for?

更新:根据 @sdedelbrock 的回答,我可以使用名称空间或房间:

UPDATE: According to the answer of @sdedelbrock I could use namespace or rooms:

  • 如果使用名称空间,则无论他们使用什么名称空间,我都无法发送给所有人.这是因为io.socketsio.of('')的快捷方式,它当然不再与名称空间匹配.
  • 这意味着我应该使用房间,但是我想知道房间和名称空间之间的语义差异是什么.
  • If use namespaces, I am not long able to send to everybody, regardless of their namespace. This is because io.sockets is a shortcut to io.of(''), which of course does not match the namespace any longer.
  • This means that I should use rooms, but I wonder what the semantic difference between a room and a namespace is.

简而言之:为什么对于同一个(?)想法有两个概念?

To cut it short: Why are there two concepts for the same (?) idea?

推荐答案

您可能正在使用房间,因此您将执行以下操作以向房间中的每个人发送消息

You could be using rooms so you would do the following to emit to everybody in a room

io.sockets.in('a').emit('inA', 'foo')

然后将其发送给您可以使用的每个人

Then to emit to everybody you can use

io.sockets.emit('everyone','bar');

您还可以使用名称空间:

You can also use namespaces as well:

io.of('/b').emit('inB', 'buzz');

要向除触发用户的用户之外的所有人发送消息,请使用

To emit to everyone except the user that triggered you would use:

io.sockets.broadcast.emit("hello");

[edit]这是更详细的答案:

名称间隔背后的想法是,它与其他名称空间(甚至是全局名称空间)分开处理.可以将其视为一个全新的socket.io实例,您可以运行新的握手,新事件,授权等,而不会干扰彼此的不同名称空间.

The idea behind name-spacing is that it is handled separately from the other namespaces (even global). Think of it as if it was an entirely new socket.io instance, you can run new handshakes, fresh events, authorizations, etc without the different namespaces interfering with each other.

这对于说/chat/tracking很有用,因为其中的连接事件具有非常不同的逻辑

This would be useful for say /chat and /tracking where the connection event would have very different logic

Socket.io为您完成所有工作,就好像它是两个单独的实例一样,但是仍然将信息限制为一个连接,这非常聪明.

Socket.io does all the work for you as if it is two separate instances, but still limits the information to one connection, which is pretty smart.

可能存在一种解决方法,您可以在其中广播到所有名称空间(下面的示例).简而言之,您不应该这样做,应该使用房间.

There might be a workaround in which you can broadcast to all the namespaces (example below). However in short you shouldn't be doing this, you should be using rooms.

for (var nameSpace in io.sockets.manager.namespaces){
  io.of(nameSpace).emit("messageToAll", message);
}

这篇关于Socket.io:命名空间,频道和合作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆