A
A
Anton Medvedev2013-04-17 21:04:32
JavaScript
Anton Medvedev, 2013-04-17 21:04:32

Dynamic namespaces in socket.io

I'm trying to implement dynamic spaces in socket.io, but I'm running into various bugs. There are many questions on the Internet on this issue, but few normal answers. Here is my implementation:

app.get '/:namespace', (req, res) ->
  namespace = '/' + req.params.namespace

  # Is already created namespace?
  namespaceExist = io.sockets.manager.namespaces[namespace]?

  # Send responce
  res.json yes

  return if namespaceExist

  # Socket.io
  io.of(namespace).on 'connection', (socket) ->
    # do something 

Customer:
$.getJSON server, {}, (yes) =>
     socket = io.connect server
     # do something  

But I ran into a bug: if it connects to the server, then restart the server (wait until the client reconnects) and try to reconnect - nothing happens.

Does anyone know how to properly organize dynamic namespaces?

Answer the question

In order to leave comments, you need to log in

1 answer(s)
N
No_Time, 2013-04-17
@Elfet

1) What prevents you from using the rooms ?
2) You can do this:

io.sockets.on('connection', function(socket){

        var namespace = '/' + socket.handshake.namespace.toLowerCase();
        
        if (!io.namespaces[namespace]){

            io.of(namespace).on('connection', function(socket){

            });
        }
});

Transfer the name of the neimspace to the authorization function and save it accordingly in the handshake.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question