socket.io房间或命名空间? [英] socket.io rooms or namespacing?

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

问题描述

我正在调查nodejs/socket.io的实时聊天,并且需要一些有关实施会议室的建议.

I am investigating nodejs/socket.io for real time chat, and I need some advice for implementing rooms.

使用名称空间或使用会议室功能来完全隔离聊天者之间的隔W,哪一种更好?

Which is better, using namespace or using the room feature to completely isolate grops of chatters from each other?

房间和名称空间之间真正的技术区别是什么?

what is the real technical difference between rooms and namespace?

资源使用上有区别吗?

推荐答案

这是名称空间和房间的共同之处 (socket.io v0.9.8-请注意,v1.0涉及一个完整的重写,所以情况可能已经改变):

This is what namespaces and rooms have in common (socket.io v0.9.8 - please note that v1.0 involved a complete rewrite, so things might have changed):

  • 两个名称空间( socket.join('room'))是在服务器端创建的
  • 多个名称空间和多个房间共享相同的(WebSocket)连接
  • 服务器将仅通过有线方式将消息传输给连接/加入nsp/room的那些客户端,即不仅仅是客户端过滤
  • Both namespaces (io.of('/nsp')) and rooms (socket.join('room')) are created on the server side
  • Multiple namespaces and multiple rooms share the same (WebSocket) connection
  • The server will transmit messages over the wire only to those clients that connected to / joined a nsp / room, i.e. it's not just client-side filtering

差异:

  • 命名空间由客户端使用io.connect(urlAndNsp)连接到(只有在服务器上已经存在客户端的情况下,客户端才会被添加到该命名空间中)
  • 只能在服务器端加入房间(尽管在服务器端创建API以使客户端能够加入很简单)
  • 命名空间可以授权受保护
  • 房间不提供授权,但是可以将自定义授权添加到服务器上上述易于创建的API中,以防万一使用房间
  • 房间是名称空间的一部分(默认为全局"名称空间)
  • 命名空间始终植根于全局范围
  • namespaces are connected to by the client using io.connect(urlAndNsp) (the client will be added to that namespace only if it already exists on the server)
  • rooms can be joined only on the server side (although creating an API on the server side to enable clients to join is straightforward)
  • namespaces can be authorization protected
  • authorization is not available with rooms, but custom authorization could be added to the aforementioned, easy-to-create API on the server, in case one is bent on using rooms
  • rooms are part of a namespace (defaulting to the 'global' namespace)
  • namespaces are always rooted in the global scope

为了不使概念与名称(房间或名称空间)混淆,我将使用 compartment 来指代该概念,并使用另外两个名称来表示实现的概念.所以如果你

To not confuse the concept with the name (room or namespace), I'll use compartment to refer to the concept, and the other two names for the implementations of the concept. So if you

  • 需要每隔室授权,名称空间可能是最简单的途径
  • 如果要分层分层的隔间(最多2层),请使用名称空间/房间组合
  • 如果您的客户端应用程序由不同部分组成(它们本身并不关心隔离专区,但是需要彼此分开),请使用名称空间.
  • need per-compartment authorization, namespaces might be the easiest route to take
  • if you want hierarchically layered compartments (2 layers max), use a namespace/room combo
  • if your client-side app consists of different parts that (do not themselves care about compartments but) need to be separated from each other, use namespaces.

后者的一个示例是一个大型客户端应用程序,其中可能分别开发(例如,第三方)的不同模块(每个模块分别使用socket.io)正在同一应用程序中使用,并希望共享一个网络连接

An example for the latter would be a large client app where different modules, perhaps developed separately (e.g. third-party), each using socket.io independently, are being used in the same app and want to share a single network connection.

尚未真正进行基准测试,在我看来,如果您只需要在项目中使用简单的隔离专区来分隔和分组消息,那么其中一个都可以.

Not having actually benchmarked this, it seems to me if you just need simple compartments in your project to separate and group messages, either one is fine.

不确定是否能回答您的问题,但是导致该问题的研究至少帮助我更加清楚了.

Not sure if that answers your question, but the research leading up to this answer at least helped me see clearer.

这篇关于socket.io房间或命名空间?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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