Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 17 additions & 0 deletions packages/socket.io/lib/client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,7 @@ export class Client<
SocketId,
Socket<ListenEvents, EmitEvents, ServerSideEvents, SocketData>
> = new Map();
private readonly connectingNsps = new Set<string>();
private nsps: Map<
string,
Socket<ListenEvents, EmitEvents, ServerSideEvents, SocketData>
Expand Down Expand Up @@ -126,6 +127,7 @@ export class Client<
* @private
*/
private connect(name: string, auth: Record<string, unknown> = {}): void {
this.connectingNsps.add(name);
if (this.server._nsps.has(name)) {
debug("connecting to namespace %s", name);
return this.doConnect(name, auth);
Expand All @@ -143,6 +145,7 @@ export class Client<
this.doConnect(name, auth);
} else {
debug("creation of namespace %s was denied", name);
this.connectingNsps.delete(name);
this._packet({
type: PacketType.CONNECT_ERROR,
nsp: name,
Expand All @@ -167,6 +170,7 @@ export class Client<
const nsp = this.server.of(name);

nsp._add(this, auth, (socket) => {
this.connectingNsps.delete(name);
this.sockets.set(socket.id, socket);
this.nsps.set(nsp.name, socket);

Expand All @@ -177,6 +181,15 @@ export class Client<
});
}

/**
* Called when a pending connection to a namespace fails.
*
* @private
*/
_onConnectFailed(name: string): void {
this.connectingNsps.delete(name);
}

/**
* Disconnects from all namespaces and closes transport.
*
Expand Down Expand Up @@ -281,6 +294,10 @@ export class Client<
const socket = this.nsps.get(namespace);

if (!socket && packet.type === PacketType.CONNECT) {
if (this.connectingNsps.has(namespace)) {
debug("already connecting to namespace %s, ignoring", namespace);
return;
}
this.connect(namespace, authPayload);
} else if (
socket &&
Expand Down
2 changes: 2 additions & 0 deletions packages/socket.io/lib/namespace.ts
Original file line number Diff line number Diff line change
Expand Up @@ -357,12 +357,14 @@ export class Namespace<
process.nextTick(() => {
if ("open" !== client.conn.readyState) {
debug("next called after client was closed - ignoring socket");
client._onConnectFailed(this.name);
socket._cleanup();
return;
}

if (err) {
debug("middleware error, sending CONNECT_ERROR packet to the client");
client._onConnectFailed(this.name);
socket._cleanup();
if (client.conn.protocol === 3) {
return socket._error(err.data || err.message);
Expand Down
47 changes: 47 additions & 0 deletions packages/socket.io/test/namespaces.ts
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,53 @@ describe("namespaces", () => {
});
});

it("should ignore a duplicate CONNECT packet while the first one is pending", (done) => {
const io = new Server(0);
const clientSocket = createClient(io, "/", { transports: ["websocket"] });
let count = 0;

io.use((socket, next) => {
// keep the first CONNECT pending while the second one is decoded
setTimeout(next, 50);
});

io.on("connection", () => {
count++;
});

clientSocket.io.engine.on("open", () => {
clientSocket.io.engine.write("0");
});

clientSocket.on("connect", () => {
setTimeout(() => {
expect(count).to.eql(1);
expect(io.of("/").sockets.size).to.eql(1);
success(done, io, clientSocket);
}, 100);
});
});

it("should allow a new CONNECT after a middleware error", (done) => {
const io = new Server(0);
const clientSocket = createClient(io, "/", { transports: ["websocket"] });
let attempts = 0;

io.use((socket, next) => {
attempts++;
next(attempts === 1 ? new Error("nope") : undefined);
});

clientSocket.on("connect_error", () => {
clientSocket.connect();
});

clientSocket.on("connect", () => {
expect(attempts).to.eql(2);
success(done, io, clientSocket);
});
});

it("should fire a `connect` event", (done) => {
const io = new Server(0);
const clientSocket = createClient(io);
Expand Down