Skip to content

Commit 02b0f73

Browse files
fix: only set 'connected' to true after middleware execution
The Socket instance is only considered connected when the "connection" event is emitted, and not during the middleware(s) execution. ```js io.use((socket, next) => { console.log(socket.connected); // prints "false" next(); }); io.on("connection", (socket) => { console.log(socket.connected); // prints "true" }); ``` Related: #4129
1 parent c0d8c5a commit 02b0f73

File tree

2 files changed

+28
-5
lines changed

2 files changed

+28
-5
lines changed

lib/socket.ts

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -126,8 +126,7 @@ export class Socket<
126126
*/
127127
public data: Partial<SocketData> = {};
128128

129-
public connected: boolean;
130-
public disconnected: boolean;
129+
public connected: boolean = false;
131130

132131
private readonly server: Server<
133132
ListenEvents,
@@ -163,8 +162,6 @@ export class Socket<
163162
} else {
164163
this.id = base64id.generateId(); // don't reuse the Engine.IO id because it's sensitive information
165164
}
166-
this.connected = true;
167-
this.disconnected = false;
168165
this.handshake = this.buildHandshake(auth);
169166
}
170167

@@ -342,6 +339,7 @@ export class Socket<
342339
*/
343340
_onconnect(): void {
344341
debug("socket connected - writing packet");
342+
this.connected = true;
345343
this.join(this.id);
346344
if (this.conn.protocol === 3) {
347345
this.packet({ type: PacketType.CONNECT });
@@ -489,7 +487,6 @@ export class Socket<
489487
this.nsp._remove(this);
490488
this.client._remove(this);
491489
this.connected = false;
492-
this.disconnected = true;
493490
this.emitReserved("disconnect", reason);
494491
return;
495492
}
@@ -631,6 +628,13 @@ export class Socket<
631628
run(0);
632629
}
633630

631+
/**
632+
* Whether the socket is currently disconnected
633+
*/
634+
public get disconnected() {
635+
return !this.connected;
636+
}
637+
634638
/**
635639
* A reference to the request that originated the underlying Engine.IO Socket.
636640
*

test/socket.io.ts

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2726,6 +2726,25 @@ describe("socket.io", () => {
27262726
if (++count === 2) done();
27272727
});
27282728
});
2729+
2730+
it("should only set `connected` to true after the middleware execution", (done) => {
2731+
const httpServer = createServer();
2732+
const io = new Server(httpServer);
2733+
2734+
const clientSocket = client(httpServer, "/");
2735+
2736+
io.use((socket, next) => {
2737+
expect(socket.connected).to.be(false);
2738+
expect(socket.disconnected).to.be(true);
2739+
next();
2740+
});
2741+
2742+
io.on("connection", (socket) => {
2743+
expect(socket.connected).to.be(true);
2744+
expect(socket.disconnected).to.be(false);
2745+
success(io, clientSocket, done);
2746+
});
2747+
});
27292748
});
27302749

27312750
describe("socket middleware", () => {

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy