Multiple socket.io connections on the same page

后端 未结 2 839
逝去的感伤
逝去的感伤 2021-01-02 03:28

Can I connect to multiple resources on the same IP and port on the client side?

I have the following code-

var myIP = \"192.168.1.1\";
var myPort = \         


        
相关标签:
2条回答
  • 2021-01-02 04:00

    Try this:

    A = io.connect(myIP+':'+myPort, {resource: 'A/socket.io', 'force new connection': true});
    B = io.connect(myIP+':'+myPort, {resource: 'B/socket.io', 'force new connection': true});
    

    (Yes, I removed some of the quotes)

    0 讨论(0)
  • 2021-01-02 04:17

    I would also mention Namespaces... a lot of times what you're trying to do is handle reconnect events on a per socket basis, namespaces do this for you. Their major upside is you don't need to have a connection per each one, which means they don't contribute to the 6-connections-per-domain limit of browsers.

    To connect, simply do this:

    socket = io('/namespace')

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