socket.io socket.on('connect'...不允许同一页面获取多个套接字
socket.io socket.on('connect'... not allowing same page to get multiple sockets
我设置了一个测试装置,试图在同一客户端和后端服务器之间创建两个套接字。打开调试后,我可以看到两个不同套接字(AAAA 和 AAAB)的 xhr 打开 GET 请求和相应的套接字接收类型为 "open" 的消息和数据的 sid 回显套接字 ID(AAAA 或 AAAB)。我在下面的跟踪中突出显示了这些。但是,这些都发生在 socket.on("connect... 调用函数之前,因此 socket.io.engine.id 仅反映第二个套接字的请求,并且该方法似乎被调用了两次。如果我猜的话,查找看到以 / 结尾的 URI 用于两个连接并正在读取后者(好像它无法区分两个未完成的调用)。我正在维护执行 io.connect 的对象中显示的 channelName这样我就可以追踪哪个套接字被回调了。
我希望跟踪的这一部分可以显示发生了什么——我还没有找到管理订阅的地方以了解它如何查找所有的侦听器...两个 io.connect 调用 (一个用于 NatA,一个用于 NatB)使用相同的 URI 和端口,并且都使用 forceNew 设置为 true:
console.log(this.channelName+": Connecting to " + uri);
this.socket = io.connect(uri,{'channelName': initEvent.channelName, 'forceNew': true});
this.socket.on("connect", function() {
console.log(self.channelName+": Setting SocketID:"+self.socket.io.engine.id);
所以,在下面的日志中,您可以看到调用 io.connect 的 NatA 和 NatB 标记,以及调用 on("connect" 的侦听器的位置,但只有第二个 NatB 套接字被调用了,而且被调用了两次...
NatA: Connecting to http://localhost:3001
socket.io-客户端:url解析http://localhost:3001+0ms
socket.io-客户端忽略 http://localhost:3001 +0ms
的套接字缓存
socket.io-client:manager readyState closed +0ms
socket.io-client:manager 打开http://localhost:3001 +1ms
engine.io-客户端:创建传输的套接字"polling" +0ms
engine.io-client:polling 轮询+1ms
engine.io-client:polling-xhr xhr poll +1ms
engine.io-client:polling-xhr xhr open GET: http://localhost:3001/socket.io/?EIO=3&transport=polling&t=1443463452620-0 +1ms
engine.io-client:polling-xhr xhr data null +1ms
engine.io-client:socket setting transport polling +1ms
socket.io-client:manager 连接尝试将在 20000 +6ms 后超时
socket.io-client:manager readyState开启+1ms
NatB:正在连接到 http://localhost:3001
socket.io-client:url解析http://localhost:3001+10ms
socket.io-客户端忽略 http://localhost:3001 +9ms
的套接字缓存
socket.io-client:manager readyState closed +2ms
socket.io-client:manager 打开http://localhost:3001 +1ms
engine.io-客户端:创建传输的套接字"polling" +5ms
engine.io-client:polling 轮询+1ms
engine.io-client:polling-xhr xhr poll +1ms
engine.io-client:polling-xhr xhr open GET: http://localhost:3001/socket.io/?EIO=3&transport=polling&t=1443463452629-1 +1ms
engine.io-client:polling-xhr xhr data null +0ms
engine.io-client:socket setting transport polling +1ms
socket.io-client:manager 连接尝试将在 20000 +6ms 后超时
socket.io-client:manager readyState开启+0ms
engine.io-client:polling polling got data ArrayBuffer +8ms
engine.io-client:socket socket receive: type "open", data "{"sid":"As6yk_kvlPxYGYdBAAAA" ,"upgrades":["websocket"],"pingInterval":25000,"pingTimeout":60000}" +4ms
engine.io-client:socket 套接字打开+1ms
socket.io-client:manager 打开+12ms
socket.io-客户端:套接字传输已打开 - 连接 +0 毫秒
engine.io-client:socket 开始升级探测 +1ms
engine.io-客户端:套接字探测传输"websocket" +0ms
engine.io-客户端:创建传输的套接字"websocket" +0ms
engine.io-client:polling 轮询+1ms
engine.io-client:polling-xhr xhr poll +0ms
engine.io-client:polling-xhr xhr open GET: http://localhost:3001/socket.io/?EIO=3&transport=polling&t=1443463452647-2&sid=As6yk_kvlPxYGYdBAAAA +1ms
engine.io-client:polling-xhr xhr data null +0ms
engine.io-client:polling polling got data ArrayBuffer +4ms
engine.io-client:socket socket receive: type "open", data "{"sid":"QRjctK24vsx9-rJzAAAB","upgrades":["websocket"],"pingInterval":25000,"pingTimeout":60000}" +1ms
engine.io-client:socket 套接字打开+0ms
socket.io-client:manager open +9ms
socket.io-客户端:套接字传输已打开 - 连接 +8 毫秒
engine.io-client:socket 开始升级探测 +1ms
engine.io-客户端:套接字探测传输"websocket" +0ms
engine.io-客户端:创建传输的套接字"websocket" +1ms
engine.io-client:polling 轮询+0ms
engine.io-client:polling-xhr xhr poll +0ms
engine.io-client:polling-xhr xhr open GET: http://localhost:3001/socket.io/?EIO=3&transport=polling&t=1443463452655-3&sid=QRjctK24vsx9-rJzAAAB +1ms
engine.io-client:polling-xhr xhr data null +0ms
engine.io-客户端:套接字探测传输"websocket" 打开+1ms
engine.io-客户端:套接字探测传输"websocket" pong +12ms
engine.io-客户端:套接字暂停当前传输"polling" +0ms
engine.io-client:polling 我们当前正在轮询 - 等待暂停 +1ms
engine.io-客户端:套接字探测传输"websocket"打开+0ms
engine.io-client:socket probe transport "websocket" pong +3ms
engine.io-客户端:套接字暂停当前传输"polling" +1ms
engine.io-client:polling 我们当前正在轮询 - 等待暂停 +0ms
engine.io-client:polling polling got data ArrayBuffer +7ms
engine.io-client:socket socket receive: type "message", data "0" +1ms
socket.io-解析器将 0 解码为 %j +0ms 对象 {type: 0, nsp: "/"}
NatB:设置 SocketID:QRjctK24vsx9-rJzAAAB
全局:发布 ChannelInitialized:{"channelName":"NatB"}
engine.io-client:polling pre-pause polling complete +3ms
engine.io-客户端:轮询暂停 +1 毫秒
engine.io-client:socket 改变传输并发送升级包+0ms
engine.io-client:socket setting transport websocket +1ms
engine.io-client:socket 清除现有传输轮询 +0ms
engine.io-client:polling ignoring poll - 传输状态"paused" +0ms
engine.io-client:polling polling got data ArrayBuffer +1ms
engine.io-client:socket socket receive: type "message", data "0" +0ms
socket.io-解析器将 0 解码为 %j +7ms 对象 {type: 0, nsp: "/"}
NatB:设置 SocketID:QRjctK24vsx9-rJzAAAB
我在您发布的代码中看不到两个套接字是如何打开的,但是如果每个套接字都在同一个 JavaScript 对象中打开并且您将两个套接字映射到 this.socket
, 然后第一个套接字在内存中被第二个套接字覆盖。
我设置了一个测试装置,试图在同一客户端和后端服务器之间创建两个套接字。打开调试后,我可以看到两个不同套接字(AAAA 和 AAAB)的 xhr 打开 GET 请求和相应的套接字接收类型为 "open" 的消息和数据的 sid 回显套接字 ID(AAAA 或 AAAB)。我在下面的跟踪中突出显示了这些。但是,这些都发生在 socket.on("connect... 调用函数之前,因此 socket.io.engine.id 仅反映第二个套接字的请求,并且该方法似乎被调用了两次。如果我猜的话,查找看到以 / 结尾的 URI 用于两个连接并正在读取后者(好像它无法区分两个未完成的调用)。我正在维护执行 io.connect 的对象中显示的 channelName这样我就可以追踪哪个套接字被回调了。
我希望跟踪的这一部分可以显示发生了什么——我还没有找到管理订阅的地方以了解它如何查找所有的侦听器...两个 io.connect 调用 (一个用于 NatA,一个用于 NatB)使用相同的 URI 和端口,并且都使用 forceNew 设置为 true:
console.log(this.channelName+": Connecting to " + uri);
this.socket = io.connect(uri,{'channelName': initEvent.channelName, 'forceNew': true});
this.socket.on("connect", function() {
console.log(self.channelName+": Setting SocketID:"+self.socket.io.engine.id);
所以,在下面的日志中,您可以看到调用 io.connect 的 NatA 和 NatB 标记,以及调用 on("connect" 的侦听器的位置,但只有第二个 NatB 套接字被调用了,而且被调用了两次...
NatA: Connecting to http://localhost:3001
socket.io-客户端:url解析http://localhost:3001+0ms
socket.io-客户端忽略 http://localhost:3001 +0ms
的套接字缓存socket.io-client:manager readyState closed +0ms
socket.io-client:manager 打开http://localhost:3001 +1ms
engine.io-客户端:创建传输的套接字"polling" +0ms
engine.io-client:polling 轮询+1ms
engine.io-client:polling-xhr xhr poll +1ms
engine.io-client:polling-xhr xhr open GET: http://localhost:3001/socket.io/?EIO=3&transport=polling&t=1443463452620-0 +1ms
engine.io-client:polling-xhr xhr data null +1ms
engine.io-client:socket setting transport polling +1ms
socket.io-client:manager 连接尝试将在 20000 +6ms 后超时
socket.io-client:manager readyState开启+1ms
NatB:正在连接到 http://localhost:3001
socket.io-client:url解析http://localhost:3001+10ms
socket.io-客户端忽略 http://localhost:3001 +9ms
的套接字缓存socket.io-client:manager readyState closed +2ms
socket.io-client:manager 打开http://localhost:3001 +1ms
engine.io-客户端:创建传输的套接字"polling" +5ms
engine.io-client:polling 轮询+1ms
engine.io-client:polling-xhr xhr poll +1ms
engine.io-client:polling-xhr xhr open GET: http://localhost:3001/socket.io/?EIO=3&transport=polling&t=1443463452629-1 +1ms
engine.io-client:polling-xhr xhr data null +0ms
engine.io-client:socket setting transport polling +1ms
socket.io-client:manager 连接尝试将在 20000 +6ms 后超时
socket.io-client:manager readyState开启+0ms
engine.io-client:polling polling got data ArrayBuffer +8ms
engine.io-client:socket socket receive: type "open", data "{"sid":"As6yk_kvlPxYGYdBAAAA" ,"upgrades":["websocket"],"pingInterval":25000,"pingTimeout":60000}" +4ms
engine.io-client:socket 套接字打开+1ms
socket.io-client:manager 打开+12ms
socket.io-客户端:套接字传输已打开 - 连接 +0 毫秒
engine.io-client:socket 开始升级探测 +1ms
engine.io-客户端:套接字探测传输"websocket" +0ms
engine.io-客户端:创建传输的套接字"websocket" +0ms
engine.io-client:polling 轮询+1ms
engine.io-client:polling-xhr xhr poll +0ms
engine.io-client:polling-xhr xhr open GET: http://localhost:3001/socket.io/?EIO=3&transport=polling&t=1443463452647-2&sid=As6yk_kvlPxYGYdBAAAA +1ms
engine.io-client:polling-xhr xhr data null +0ms
engine.io-client:polling polling got data ArrayBuffer +4ms
engine.io-client:socket socket receive: type "open", data "{"sid":"QRjctK24vsx9-rJzAAAB","upgrades":["websocket"],"pingInterval":25000,"pingTimeout":60000}" +1ms
engine.io-client:socket 套接字打开+0ms
socket.io-client:manager open +9ms
socket.io-客户端:套接字传输已打开 - 连接 +8 毫秒
engine.io-client:socket 开始升级探测 +1ms
engine.io-客户端:套接字探测传输"websocket" +0ms
engine.io-客户端:创建传输的套接字"websocket" +1ms
engine.io-client:polling 轮询+0ms
engine.io-client:polling-xhr xhr poll +0ms
engine.io-client:polling-xhr xhr open GET: http://localhost:3001/socket.io/?EIO=3&transport=polling&t=1443463452655-3&sid=QRjctK24vsx9-rJzAAAB +1ms
engine.io-client:polling-xhr xhr data null +0ms
engine.io-客户端:套接字探测传输"websocket" 打开+1ms
engine.io-客户端:套接字探测传输"websocket" pong +12ms
engine.io-客户端:套接字暂停当前传输"polling" +0ms
engine.io-client:polling 我们当前正在轮询 - 等待暂停 +1ms
engine.io-客户端:套接字探测传输"websocket"打开+0ms
engine.io-client:socket probe transport "websocket" pong +3ms
engine.io-客户端:套接字暂停当前传输"polling" +1ms
engine.io-client:polling 我们当前正在轮询 - 等待暂停 +0ms
engine.io-client:polling polling got data ArrayBuffer +7ms
engine.io-client:socket socket receive: type "message", data "0" +1ms
socket.io-解析器将 0 解码为 %j +0ms 对象 {type: 0, nsp: "/"}
NatB:设置 SocketID:QRjctK24vsx9-rJzAAAB
全局:发布 ChannelInitialized:{"channelName":"NatB"}
engine.io-client:polling pre-pause polling complete +3ms
engine.io-客户端:轮询暂停 +1 毫秒
engine.io-client:socket 改变传输并发送升级包+0ms
engine.io-client:socket setting transport websocket +1ms
engine.io-client:socket 清除现有传输轮询 +0ms
engine.io-client:polling ignoring poll - 传输状态"paused" +0ms
engine.io-client:polling polling got data ArrayBuffer +1ms
engine.io-client:socket socket receive: type "message", data "0" +0ms
socket.io-解析器将 0 解码为 %j +7ms 对象 {type: 0, nsp: "/"}
NatB:设置 SocketID:QRjctK24vsx9-rJzAAAB
我在您发布的代码中看不到两个套接字是如何打开的,但是如果每个套接字都在同一个 JavaScript 对象中打开并且您将两个套接字映射到 this.socket
, 然后第一个套接字在内存中被第二个套接字覆盖。