File tree Expand file tree Collapse file tree 2 files changed +40
-2
lines changed Expand file tree Collapse file tree 2 files changed +40
-2
lines changed Original file line number Diff line number Diff line change @@ -144,6 +144,8 @@ export class Socket<
144
144
#anyIncomingListeners?: Array < ( ...args : Event ) => void > ;
145
145
#anyOutgoingListeners?: Array < ( ...args : Event ) => void > ;
146
146
147
+ #preConnectBuffer: Packet [ ] = [ ] ;
148
+
147
149
/* private */ readonly client : Client <
148
150
ListenEvents ,
149
151
EmitEvents ,
@@ -197,8 +199,12 @@ export class Socket<
197
199
const flags = Object . assign ( { } , this . flags ) ;
198
200
this . flags = { } ;
199
201
200
- this . _notifyOutgoingListeners ( packet . data ) ;
201
- this . packet ( packet , flags ) ;
202
+ if ( this . connected ) {
203
+ this . _notifyOutgoingListeners ( packet . data ) ;
204
+ this . packet ( packet , flags ) ;
205
+ } else {
206
+ this . #preConnectBuffer. push ( packet ) ;
207
+ }
202
208
203
209
return true ;
204
210
}
@@ -514,6 +520,11 @@ export class Socket<
514
520
this . connected = true ;
515
521
this . join ( this . id ) ;
516
522
this . packet ( { type : PacketType . CONNECT , data : { sid : this . id } } ) ;
523
+ this . #preConnectBuffer. forEach ( ( packet ) => {
524
+ this . _notifyOutgoingListeners ( packet . data ) ;
525
+ this . packet ( packet ) ;
526
+ } ) ;
527
+ this . #preConnectBuffer = [ ] ;
517
528
}
518
529
519
530
/**
Original file line number Diff line number Diff line change @@ -10,6 +10,7 @@ import {
10
10
eioPush ,
11
11
enableLogs ,
12
12
parseSessionID ,
13
+ runHandshake ,
13
14
} from "../../util.test.ts" ;
14
15
import { setup } from "./setup.test.ts" ;
15
16
@@ -236,4 +237,30 @@ describe("handshake", () => {
236
237
} ,
237
238
) ;
238
239
} ) ;
240
+
241
+ it ( "should complete handshake before sending any event" , ( ) => {
242
+ const io = new Server ( ) ;
243
+
244
+ return setup (
245
+ io ,
246
+ 1 ,
247
+ async ( port , done ) => {
248
+ io . use ( ( socket ) => {
249
+ socket . emit ( "1" ) ;
250
+ io . emit ( "ignored" ) ; // socket is not connected yet
251
+ return Promise . resolve ( ) ;
252
+ } ) ;
253
+
254
+ io . on ( "connection" , ( socket ) => {
255
+ socket . emit ( "2" ) ;
256
+ } ) ;
257
+
258
+ const [ _ , firstPacket ] = await runHandshake ( port ) ;
259
+
260
+ assertEquals ( firstPacket , '42["1"]\x1e42["2"]' ) ;
261
+
262
+ done ( ) ;
263
+ } ,
264
+ ) ;
265
+ } ) ;
239
266
} ) ;
You can’t perform that action at this time.
0 commit comments