【发布时间】:2015-12-26 04:21:31
【问题描述】:
我设置了一个测试平台,尝试在同一个客户端和后端服务器之间创建两个套接字。打开调试后,我可以看到针对两个不同套接字(AAAA 和 AAAB)的 xhr open GET 请求,并且相应的套接字接收类型为“open”的消息,并且数据的 sid 回显套接字 id(AAAA 或 AAAB)。我在下面的跟踪中突出显示了这些。但是,这些都发生在调用 socket.on("connect... 函数之前,因此 socket.io.engine.id 只反映了第二个套接字的请求,并且该方法似乎被调用了两次。如果我猜的话查找看到以 / 结尾的 URI,它用于两个连接并且正在读取后者(好像它无法区分两个未完成的调用)。我正在维护执行 io.connect 的对象中显示的通道名称,所以我可以追踪哪个套接字被回调。
我希望跟踪的这一部分可能会显示发生了什么——我还没有找到管理订阅的位置以了解它如何查找所有的侦听器...两个 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:连接到http://localhost:3001
socket.io-client:url 解析http://localhost:3001+0ms
socket.io-client 忽略 http://localhost:3001 +0ms 的套接字缓存
socket.io-client:manager readyState 关闭 +0ms
socket.io-client:manager 开启http://localhost:3001 +1ms
engine.io-client:socket 创建传输“轮询”+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设置传输轮询+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-client 忽略 http://localhost:3001 +9ms 的套接字缓存
socket.io-client:manager readyState 关闭 +2ms
socket.io-client:manager 开启http://localhost:3001 +1ms
engine.io-client:socket 创建传输“轮询”+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设置传输轮询+1ms
socket.io-client:manager 连接尝试将在 20000 +6ms 后超时
socket.io-client:manager readyState 开启 +0ms
engine.io-client:polling 轮询得到数据ArrayBuffer +8ms
engine.io-client:socket socket 接收:类型 "open",数据 "{"sid":"As6yk_kvlPxYGYdBAAAA","upgrades":["websocket"],"pingInterval": 25000,"pingTimeout":60000}" +4ms
engine.io-client:socket 套接字打开 +1ms
socket.io-client:manager open +12ms
socket.io-client:socket 传输打开 - 连接 +0ms
engine.io-client:socket 开始升级探测 +1ms
engine.io-client:socket 探测传输“websocket”+0ms
engine.io-client:socket 创建传输“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 轮询得到数据ArrayBuffer +4ms
engine.io-client:socket socket 接收:类型“open”,数据“{“sid”:“QRjctK24vsx9-rJzAAAB”,“upgrades”:[“websocket”],“pingInterval ":25000,"pingTimeout":60000}" +1ms
engine.io-client:socket 套接字打开 +0ms
socket.io-client:manager open +9ms
socket.io-client:socket 传输打开 - 连接 +8ms
engine.io-client:socket 开始升级探测 +1ms
engine.io-client:socket 探测传输“websocket”+0ms
engine.io-client:socket 创建传输“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-client:socket 探测传输“websocket”打开+1ms
engine.io-client:socket 探测传输“websocket”pong +12ms
engine.io-client:socket 暂停当前传输“轮询”+0ms
engine.io-client:polling 我们当前正在轮询 - 等待暂停 +1ms
engine.io-client:socket 探测传输“websocket”打开+0ms
engine.io-client:socket 探测传输“websocket”pong +3ms
engine.io-client:socket 暂停当前传输“轮询”+1ms
engine.io-client:polling 我们当前正在轮询 - 等待暂停 +0ms
engine.io-client:polling 轮询得到数据ArrayBuffer +7ms
engine.io-client:socket socket 接收:type "message", data "0" +1ms
socket.io-parser 将 0 解码为 %j +0ms Object {type: 0, nsp: "/"}
NatB:设置 SocketID:QRjctK24vsx9-rJzAAAB
全局:发布 ChannelInitialized:{"channelName":"NatB"}
engine.io-client:polling pre-pause polling 完成 +3ms
engine.io-client:轮询暂停 +1 毫秒
engine.io-client:socket改变传输和发送升级包+0ms
engine.io-client:socket设置传输websocket +1ms
engine.io-client:socket 清除现有传输轮询 +0ms
engine.io-client:polling ignoring poll - 传输状态“暂停”+0ms
engine.io-client:polling 轮询得到数据ArrayBuffer +1ms
engine.io-client:socket socket 接收:type "message", data "0" +0ms
socket.io-parser 将 0 解码为 %j +7ms Object {type: 0, nsp: "/"}
NatB:设置 SocketID:QRjctK24vsx9-rJzAAAB
【问题讨论】:
-
用最新的 socket.io 1.3.7 和 1.2.1 测试
标签: javascript socket.io