wnm3
wnm3

Reputation: 421

socket.io socket.on('connect'... not allowing same page to get multiple sockets

I have a test rig set up that attempts to create two sockets between the same client and a backend server. With debugging turned on, I can see the xhr open GET requests for two different sockets (AAAA, and AAAB) and the corresponding socket receive messages with type "open" and the data's sid echoing the socket id (AAAA or AAAB). I've highlighted these in the trace below. However, these both occur before the socket.on("connect... function is called so the socket.io.engine.id reflects only the 2nd socket's request and that method appears to get called twice. If I were to guess, the lookup is seeing the URI ending in / that was used for both connections and is reading the latter (as if it can't distinguish between two outstanding calls). I am maintaining the channelName shown in an object that performs the io.connect so I can trace which socket is being called back.

I would hope this part of the trace might show what occurred -- I haven't found where the subscriptions are managed to understand how it looks up which listener to all... Both io.connect calls (one for NatA and one for NatB) use the same URI and port and both use forceNew set to 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);

so, in the log below, you can see the NatA and NatB tags where the io.connect was called, and where the listener for on("connect" is called but only the 2nd NatB socket is called, and it is called twice...

NatA: Connecting to http://localhost:3001

socket.io-client:url parse http://localhost:3001 +0ms

socket.io-client ignoring socket cache for http://localhost:3001 +0ms

socket.io-client:manager readyState closed +0ms

socket.io-client:manager opening http://localhost:3001 +1ms

engine.io-client:socket creating transport "polling" +0ms

engine.io-client:polling 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 connect attempt will timeout after 20000 +6ms

socket.io-client:manager readyState opening +1ms

NatB: Connecting to http://localhost:3001

socket.io-client:url parse http://localhost:3001 +10ms

socket.io-client ignoring socket cache for http://localhost:3001 +9ms

socket.io-client:manager readyState closed +2ms

socket.io-client:manager opening http://localhost:3001 +1ms

engine.io-client:socket creating transport "polling" +5ms

engine.io-client:polling 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 connect attempt will timeout after 20000 +6ms

socket.io-client:manager readyState opening +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 socket open +1ms

socket.io-client:manager open +12ms

socket.io-client:socket transport is open - connecting +0ms

engine.io-client:socket starting upgrade probes +1ms

engine.io-client:socket probing transport "websocket" +0ms

engine.io-client:socket creating transport "websocket" +0ms

engine.io-client:polling 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 socket open +0ms

socket.io-client:manager open +9ms

socket.io-client:socket transport is open - connecting +8ms

engine.io-client:socket starting upgrade probes +1ms

engine.io-client:socket probing transport "websocket" +0ms

engine.io-client:socket creating transport "websocket" +1ms

engine.io-client:polling 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 probe transport "websocket" opened +1ms

engine.io-client:socket probe transport "websocket" pong +12ms

engine.io-client:socket pausing current transport "polling" +0ms

engine.io-client:polling we are currently polling - waiting to pause +1ms

engine.io-client:socket probe transport "websocket" opened +0ms

engine.io-client:socket probe transport "websocket" pong +3ms

engine.io-client:socket pausing current transport "polling" +1ms

engine.io-client:polling we are currently polling - waiting to pause +0ms

engine.io-client:polling polling got data ArrayBuffer +7ms

engine.io-client:socket socket receive: type "message", data "0" +1ms

socket.io-parser decoded 0 as %j +0ms Object {type: 0, nsp: "/"}

NatB: Setting SocketID:QRjctK24vsx9-rJzAAAB

global: publishing ChannelInitialized:{"channelName":"NatB"}

engine.io-client:polling pre-pause polling complete +3ms

engine.io-client:polling paused +1ms

engine.io-client:socket changing transport and sending upgrade packet +0ms

engine.io-client:socket setting transport websocket +1ms

engine.io-client:socket clearing existing transport polling +0ms

engine.io-client:polling ignoring poll - transport state "paused" +0ms

engine.io-client:polling polling got data ArrayBuffer +1ms

engine.io-client:socket socket receive: type "message", data "0" +0ms

socket.io-parser decoded 0 as %j +7ms Object {type: 0, nsp: "/"}

NatB: Setting SocketID:QRjctK24vsx9-rJzAAAB

Upvotes: 0

Views: 876

Answers (1)

SalientGreen
SalientGreen

Reputation: 5353

I don't see in the code you posted how two sockets are being opened, but if each socket is being opened inside the same JavaScript object and you are mapping both sockets to this.socket, then the first socket is being overwritten in memory by the second.

Upvotes: 1

Related Questions