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

查看:23
本文介绍了同一页面上的多个 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 上运行 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(message) 回调接收到实际属于 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天全站免登陆