同一页面上有多个socket.io连接 [英] Multiple socket.io connections on the same page

查看:576
本文介绍了同一页面上有多个socket.io连接的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我可以连接到客户端上同一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上的 on(消息)回调接收相同的数据它实际上属于A。

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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆