File tree 8 files changed +24
-79
lines changed
8 files changed +24
-79
lines changed Original file line number Diff line number Diff line change @@ -35,20 +35,6 @@ export declare class Manager extends Emitter {
35
35
* @api private
36
36
*/
37
37
emitAll ( event : string , arg ?: any ) : void ;
38
- /**
39
- * Update `socket.id` of all sockets
40
- *
41
- * @api private
42
- */
43
- updateSocketIds ( ) : void ;
44
- /**
45
- * generate `socket.id` for the given `nsp`
46
- *
47
- * @param {String } nsp
48
- * @return {String }
49
- * @api private
50
- */
51
- generateId ( nsp : any ) : string ;
52
38
/**
53
39
* Sets the `reconnection` config.
54
40
*
Original file line number Diff line number Diff line change @@ -91,28 +91,6 @@ class Manager extends component_emitter_1.default {
91
91
}
92
92
}
93
93
}
94
- /**
95
- * Update `socket.id` of all sockets
96
- *
97
- * @api private
98
- */
99
- updateSocketIds ( ) {
100
- for ( let nsp in this . nsps ) {
101
- if ( has . call ( this . nsps , nsp ) ) {
102
- this . nsps [ nsp ] . id = this . generateId ( nsp ) ;
103
- }
104
- }
105
- }
106
- /**
107
- * generate `socket.id` for the given `nsp`
108
- *
109
- * @param {String } nsp
110
- * @return {String }
111
- * @api private
112
- */
113
- generateId ( nsp ) {
114
- return ( nsp === "/" ? "" : nsp + "#" ) + this . engine . id ;
115
- }
116
94
/**
117
95
* Sets the `reconnection` config.
118
96
*
@@ -386,9 +364,6 @@ class Manager extends component_emitter_1.default {
386
364
this . nsps [ nsp ] = socket ;
387
365
var self = this ;
388
366
socket . on ( "connecting" , onConnecting ) ;
389
- socket . on ( "connect" , function ( ) {
390
- socket . id = self . generateId ( nsp ) ;
391
- } ) ;
392
367
if ( this . autoConnect ) {
393
368
// manually call here since connecting event is fired before listening
394
369
onConnecting ( ) ;
@@ -572,7 +547,6 @@ class Manager extends component_emitter_1.default {
572
547
const attempt = this . backoff . attempts ;
573
548
this . reconnecting = false ;
574
549
this . backoff . reset ( ) ;
575
- this . updateSocketIds ( ) ;
576
550
this . emitAll ( "reconnect" , attempt ) ;
577
551
}
578
552
}
Original file line number Diff line number Diff line change @@ -100,7 +100,7 @@ export declare class Socket extends Emitter {
100
100
*
101
101
* @api private
102
102
*/
103
- onconnect ( ) : void ;
103
+ onconnect ( id : string ) : void ;
104
104
/**
105
105
* Emit buffered events (received and emitted).
106
106
*
Original file line number Diff line number Diff line change @@ -203,7 +203,8 @@ class Socket extends component_emitter_1.default {
203
203
return ;
204
204
switch ( packet . type ) {
205
205
case socket_io_parser_1 . PacketType . CONNECT :
206
- this . onconnect ( ) ;
206
+ const id = packet . data . sid ;
207
+ this . onconnect ( id ) ;
207
208
break ;
208
209
case socket_io_parser_1 . PacketType . EVENT :
209
210
this . onevent ( packet ) ;
@@ -289,7 +290,8 @@ class Socket extends component_emitter_1.default {
289
290
*
290
291
* @api private
291
292
*/
292
- onconnect ( ) {
293
+ onconnect ( id ) {
294
+ this . id = id ;
293
295
this . connected = true ;
294
296
this . disconnected = false ;
295
297
this . emit ( "connect" ) ;
Original file line number Diff line number Diff line change @@ -93,30 +93,6 @@ export class Manager extends Emitter {
93
93
}
94
94
}
95
95
96
- /**
97
- * Update `socket.id` of all sockets
98
- *
99
- * @api private
100
- */
101
- updateSocketIds ( ) {
102
- for ( let nsp in this . nsps ) {
103
- if ( has . call ( this . nsps , nsp ) ) {
104
- this . nsps [ nsp ] . id = this . generateId ( nsp ) ;
105
- }
106
- }
107
- }
108
-
109
- /**
110
- * generate `socket.id` for the given `nsp`
111
- *
112
- * @param {String } nsp
113
- * @return {String }
114
- * @api private
115
- */
116
- generateId ( nsp ) {
117
- return ( nsp === "/" ? "" : nsp + "#" ) + this . engine . id ;
118
- }
119
-
120
96
/**
121
97
* Sets the `reconnection` config.
122
98
*
@@ -417,9 +393,6 @@ export class Manager extends Emitter {
417
393
this . nsps [ nsp ] = socket ;
418
394
var self = this ;
419
395
socket . on ( "connecting" , onConnecting ) ;
420
- socket . on ( "connect" , function ( ) {
421
- socket . id = self . generateId ( nsp ) ;
422
- } ) ;
423
396
424
397
if ( this . autoConnect ) {
425
398
// manually call here since connecting event is fired before listening
@@ -618,7 +591,6 @@ export class Manager extends Emitter {
618
591
const attempt = this . backoff . attempts ;
619
592
this . reconnecting = false ;
620
593
this . backoff . reset ( ) ;
621
- this . updateSocketIds ( ) ;
622
594
this . emitAll ( "reconnect" , attempt ) ;
623
595
}
624
596
}
Original file line number Diff line number Diff line change @@ -223,7 +223,8 @@ export class Socket extends Emitter {
223
223
224
224
switch ( packet . type ) {
225
225
case PacketType . CONNECT :
226
- this . onconnect ( ) ;
226
+ const id = packet . data . sid ;
227
+ this . onconnect ( id ) ;
227
228
break ;
228
229
229
230
case PacketType . EVENT :
@@ -319,7 +320,8 @@ export class Socket extends Emitter {
319
320
*
320
321
* @api private
321
322
*/
322
- onconnect ( ) {
323
+ onconnect ( id : string ) {
324
+ this . id = id ;
323
325
this . connected = true ;
324
326
this . disconnected = false ;
325
327
this . emit ( "connect" ) ;
Original file line number Diff line number Diff line change @@ -6,19 +6,22 @@ describe("socket", function () {
6
6
7
7
it ( "should have an accessible socket id equal to the server-side socket id (default namespace)" , ( done ) => {
8
8
const socket = io ( { forceNew : true } ) ;
9
- socket . on ( "connect" , ( ) => {
9
+
10
+ socket . emit ( "getId" , ( id ) => {
10
11
expect ( socket . id ) . to . be . ok ( ) ;
11
- expect ( socket . id ) . to . eql ( socket . io . engine . id ) ;
12
+ expect ( socket . id ) . to . be . eql ( id ) ;
13
+ expect ( socket . id ) . to . not . eql ( socket . io . engine . id ) ;
12
14
socket . disconnect ( ) ;
13
15
done ( ) ;
14
16
} ) ;
15
17
} ) ;
16
18
17
19
it ( "should have an accessible socket id equal to the server-side socket id (custom namespace)" , ( done ) => {
18
20
const socket = io ( "/foo" , { forceNew : true } ) ;
19
- socket . on ( "connect ", ( ) => {
21
+ socket . emit ( "getId ", ( id ) => {
20
22
expect ( socket . id ) . to . be . ok ( ) ;
21
- expect ( socket . id ) . to . eql ( "/foo#" + socket . io . engine . id ) ;
23
+ expect ( socket . id ) . to . be . eql ( id ) ;
24
+ expect ( socket . id ) . to . not . eql ( socket . io . engine . id ) ;
22
25
socket . disconnect ( ) ;
23
26
done ( ) ;
24
27
} ) ;
Original file line number Diff line number Diff line change @@ -4,8 +4,10 @@ const io = require("socket.io");
4
4
const server = io ( process . env . ZUUL_PORT || 3210 , { pingInterval : 2000 } ) ;
5
5
const expect = require ( "expect.js" ) ;
6
6
7
- server . of ( "/foo" ) . on ( "connection" , ( ) => {
8
- // register namespace
7
+ server . of ( "/foo" ) . on ( "connection" , ( socket ) => {
8
+ socket . on ( "getId" , ( cb ) => {
9
+ cb ( socket . id ) ;
10
+ } ) ;
9
11
} ) ;
10
12
11
13
server . of ( "/timeout_socket" ) . on ( "connection" , ( ) => {
@@ -145,4 +147,8 @@ server.on("connection", (socket) => {
145
147
socket . on ( "getHandshake" , ( cb ) => {
146
148
cb ( socket . handshake ) ;
147
149
} ) ;
150
+
151
+ socket . on ( "getId" , ( cb ) => {
152
+ cb ( socket . id ) ;
153
+ } ) ;
148
154
} ) ;
You can’t perform that action at this time.
0 commit comments