简体   繁体   中英

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. Use socket.nsp.name to get the actual 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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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