简体   繁体   English

Socket.io获取套接字所属的命名空间

[英]Socket.io get the namespace a socket belongs to

Is there a way to get a sockets namespace? 有没有办法获得套接字命名空间? I've tried various things but cannot get it to work. 我尝试了各种各样的东西,但无法让它发挥作用。 Given a socket object I would like to be able to know what namespace it belongs to. 给定一个套接字对象,我希望能够知道它属于哪个命名空间。 Thank you very much. 非常感谢你。

for example 例如

nsp = io.of('/' + venue_code).on('connection', function(socket) {
    socket.namespace = nsp
    if (typeof servers[nsp.name] == 'undefined') {
        servers[nsp.name] = socket.id
        winston.debug("Server " + socket.id + " connected to " + nsp.name)
        socket.room = "servers"
        socket.join(socket.room)
    } else {
        socket.room = "clients"
        socket.join(socket.room)
        winston.debug("Client " + socket.id + " connected to " + nsp.name)
    }

It is just socket.nsp , which gives the entire Namespace object. 它只是socket.nsp ,它提供整个Namespace对象。 Use socket.nsp.name to get the actual name. 使用socket.nsp.name获取实际名称。

nsp = io.of('/' + venue_code).on('connection', function(socket) {
    winston.debug(socket.nsp)
}

Note that if you attach this same handler to the root namespace, you'll get the root namespace -- those middlewares are run before the correct namespace is attached. 请注意,如果将此相同的处理程序附加到根命名空间,您将获得根命名空间 - 这些中间件连接正确的命名空间之前运行。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM