javascript - Socket.io get the namespace a socket belongs to - Stack Overflow

Is there a way to get a sockets namespace? I've tried various things but cannot get it to work. Gi

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)
    }

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)
    }
Share Improve this question asked Jul 24, 2015 at 2:25 patrick_corriganpatrick_corrigan 89911 silver badges25 bronze badges
Add a ment  | 

1 Answer 1

Reset to default 11

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.

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1743738298a4498666.html

相关推荐

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信