同一页面上有多个socket.io连接 [英] Multiple socket.io connections on the same page
问题描述
我可以连接到客户端上同一IP和端口上的多个资源吗?
Can I connect to multiple resources on the same IP and port on the client side?
我有以下代码 -
var myIP = "192.168.1.1";
var myPort = "8080";
A = io.connect(myIP+':'+myPort,{resource: 'A/socket.io'});
B = io.connect(myIP+':'+myPort,{resource: 'B/socket.io'});
A.on('connect',console.log('A connected');
B.on('connect',console.log('B connected');
A.on('message',function(d){console.log('A: '+ d);}
B.on('message',function(d){console.log('B: '+ d);}
我正在运行 myIP:myPort ://github.com/nodejitsu/node-http-proxy> node-http-proxy 。
代理A和B上的连接到它们各自的socket-io服务器。
I am running node-http-proxy on myIP:myPort
.
It is proxying connections on A and B to their respective socket-io servers.
如果我在一个脚本上运行上面的代码,浏览器会忽略第二个语句(它不会触发对资源B)的请求。
If I run the above code on a single script, the browser ignores the second statement (It does not fires a request to resource B).
A和B上的
The on(message)
callbacks for both A and B recieve the same data which actually belongs to A.
如果我在两个不同的html页面中运行上面的代码(A在一个而B在另一个上),它们工作正常,我得到了数据单独使用。
If I run the above code in two different html pages (A on one and B on other), they work fine and I get the data for both separately.
推荐答案
试试这个:
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)
这篇关于同一页面上有多个socket.io连接的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!