io.on('connection',…) vs io.sockets.on('connection',…)

后端 未结 1 1193
再見小時候
再見小時候 2020-12-28 13:29

I am using socket.io and the Mean stack for a web app. I started the server for socket on 3006 port..

var http = require(\'http\').createServer(app);
http.li         


        
1条回答
  •  慢半拍i
    慢半拍i (楼主)
    2020-12-28 13:38

    The default namespace that Socket.IO clients connect to by default is: /. It is identified by io.sockets or simply io (docs).

    This example copied from the documentation:

    // the following two will emit to all the sockets connected to `/`
    
    io.sockets.emit('hi', 'everyone');
    
    io.emit('hi', 'everyone');           // short form
    

    I assume it is the same for 'on', as it is for 'emit': using 'io.sockets' is equivalent to using 'io' only, it's just a shorter form.

    To “namespace” your sockets, means assigning different endpoints or paths (which can be useful).

    From an answer to this SO question:

    "Socket.io does all the work for you as if it is two separate instances, but still limits the information to one connection, which is pretty smart."

    0 讨论(0)
提交回复
热议问题