socket.io icon indicating copy to clipboard operation
socket.io copied to clipboard

Auth duplicates connect messages

Open constantant opened this issue 2 years ago • 1 comments

Describe the bug In case calling Manager's method socket with the auth data it emits two auth messages which causes an error

To Reproduce

Please fill the following code example:

Socket.IO client version: 4.7.1

Client

import { Manager } from "socket.io-client";

const manager = new Manager("ws://localhost:3000/", {transports: ['websocket'], upgrade: false});

const socket = manager.socket('my-namespace', { auth: { 'X-Auth': 'XXX-XXX-XXX' } });

socket.on("connect", () => {
  // at this moment it is already failed
  console.log(`connect ${socket.id}`);
});

socket.on("disconnect", () => {
  console.log("disconnect");
});

Expected behavior At moment a first connect callback is set the socket must be 100% connected.

Platform:

  • Device: Dell Latitude
  • OS: Windows 10

Workaround https://github.com/socketio/socket.io/issues/474#issuecomment-1614530797

constantant avatar Jun 30 '23 12:06 constantant

Hi! I think that's because you are missing a "/" at the beginning of the namespace, it should rather be:

const socket = manager.socket('/my-namespace', { auth: { 'X-Auth': 'XXX-XXX-XXX' } });

Output:

$ DEBUG=socket* node client.js 
  socket.io-client:manager readyState closed +0ms
  socket.io-client:manager opening ws://localhost:3000/ +1ms
  socket.io-client:manager connect attempt will timeout after 20000 +6ms
  socket.io-client:manager readyState opening +0ms
  socket.io-client:manager open +9ms
  socket.io-client:manager cleanup +0ms
  socket.io-client:socket transport is open - connecting +0ms
  socket.io-client:manager writing packet {"type":0,"data":{"X-Auth":"XXX-XXX-XXX"},"nsp":"/my-namespace"} +0ms
  socket.io-parser encoding packet {"type":0,"data":{"X-Auth":"XXX-XXX-XXX"},"nsp":"/my-namespace"} +0ms
  socket.io-parser encoded {"type":0,"data":{"X-Auth":"XXX-XXX-XXX"},"nsp":"/my-namespace"} as 0/my-namespace,{"X-Auth":"XXX-XXX-XXX"} +0ms
  socket.io-parser decoded 0/my-namespace,{"sid":"qU0TCPiLzrIsNVEyAAAB"} as {"type":0,"nsp":"/my-namespace","data":{"sid":"qU0TCPiLzrIsNVEyAAAB"}} +4ms
  socket.io-client:socket socket connected with id qU0TCPiLzrIsNVEyAAAB +4ms
connect qU0TCPiLzrIsNVEyAAAB
  socket.io-client:socket draining queue +1ms

darrachequesne avatar Jul 08 '23 06:07 darrachequesne