2
2
3
3
import io .socket .backo .Backoff ;
4
4
import io .socket .emitter .Emitter ;
5
+ import io .socket .parser .DecodingException ;
5
6
import io .socket .parser .IOParser ;
6
7
import io .socket .parser .Packet ;
7
8
import io .socket .parser .Parser ;
10
11
import okhttp3 .WebSocket ;
11
12
12
13
import java .net .URI ;
13
- import java .util .ArrayList ;
14
- import java .util .Date ;
15
- import java .util .HashSet ;
16
- import java .util .LinkedList ;
17
- import java .util .List ;
18
- import java .util .Map ;
19
- import java .util .Queue ;
20
- import java .util .Set ;
21
- import java .util .Timer ;
22
- import java .util .TimerTask ;
14
+ import java .util .*;
23
15
import java .util .concurrent .ConcurrentHashMap ;
24
16
import java .util .logging .Level ;
25
17
import java .util .logging .Logger ;
@@ -48,16 +40,6 @@ public class Manager extends Emitter {
48
40
public static final String EVENT_PACKET = "packet" ;
49
41
public static final String EVENT_ERROR = "error" ;
50
42
51
- /**
52
- * Called on a connection error.
53
- */
54
- public static final String EVENT_CONNECT_ERROR = "connect_error" ;
55
-
56
- /**
57
- * Called on a connection timeout.
58
- */
59
- public static final String EVENT_CONNECT_TIMEOUT = "connect_timeout" ;
60
-
61
43
/**
62
44
* Called on a successful reconnection.
63
45
*/
@@ -72,12 +54,6 @@ public class Manager extends Emitter {
72
54
73
55
public static final String EVENT_RECONNECT_ATTEMPT = "reconnect_attempt" ;
74
56
75
- public static final String EVENT_RECONNECTING = "reconnecting" ;
76
-
77
- public static final String EVENT_PING = "ping" ;
78
-
79
- public static final String EVENT_PONG = "pong" ;
80
-
81
57
/**
82
58
* Called when a new transport is created. (experimental)
83
59
*/
@@ -98,8 +74,6 @@ public class Manager extends Emitter {
98
74
private double _randomizationFactor ;
99
75
private Backoff backoff ;
100
76
private long _timeout ;
101
- private Set <Socket > connecting = new HashSet <Socket >();
102
- private Date lastPing ;
103
77
private URI uri ;
104
78
private List <Packet > packetBuffer ;
105
79
private Queue <On .Handle > subs ;
@@ -160,28 +134,6 @@ public Manager(URI uri, Options opts) {
160
134
this .decoder = opts .decoder != null ? opts .decoder : new IOParser .Decoder ();
161
135
}
162
136
163
- private void emitAll (String event , Object ... args ) {
164
- this .emit (event , args );
165
- for (Socket socket : this .nsps .values ()) {
166
- socket .emit (event , args );
167
- }
168
- }
169
-
170
- /**
171
- * Update `socket.id` of all sockets
172
- */
173
- private void updateSocketIds () {
174
- for (Map .Entry <String , Socket > entry : this .nsps .entrySet ()) {
175
- String nsp = entry .getKey ();
176
- Socket socket = entry .getValue ();
177
- socket .id = this .generateId (nsp );
178
- }
179
- }
180
-
181
- private String generateId (String nsp ) {
182
- return ("/" .equals (nsp ) ? "" : (nsp + "#" )) + this .engine .id ();
183
- }
184
-
185
137
public boolean reconnection () {
186
138
return this ._reconnection ;
187
139
}
@@ -307,7 +259,7 @@ public void call(Object... objects) {
307
259
logger .fine ("connect_error" );
308
260
self .cleanup ();
309
261
self .readyState = ReadyState .CLOSED ;
310
- self .emitAll ( EVENT_CONNECT_ERROR , data );
262
+ self .emit ( EVENT_ERROR , data );
311
263
if (fn != null ) {
312
264
Exception err = new SocketIOException ("Connection error" ,
313
265
data instanceof Exception ? (Exception ) data : null );
@@ -334,7 +286,6 @@ public void run() {
334
286
openSub .destroy ();
335
287
socket .close ();
336
288
socket .emit (Engine .EVENT_ERROR , new SocketIOException ("timeout" ));
337
- self .emitAll (EVENT_CONNECT_TIMEOUT , timeout );
338
289
}
339
290
});
340
291
}
@@ -377,18 +328,6 @@ public void call(Object... objects) {
377
328
}
378
329
}
379
330
}));
380
- this .subs .add (On .on (socket , Engine .EVENT_PING , new Listener () {
381
- @ Override
382
- public void call (Object ... objects ) {
383
- Manager .this .onping ();
384
- }
385
- }));
386
- this .subs .add (On .on (socket , Engine .EVENT_PONG , new Listener () {
387
- @ Override
388
- public void call (Object ... objects ) {
389
- Manager .this .onpong ();
390
- }
391
- }));
392
331
this .subs .add (On .on (socket , Engine .EVENT_ERROR , new Listener () {
393
332
@ Override
394
333
public void call (Object ... objects ) {
@@ -409,22 +348,20 @@ public void call (Packet packet) {
409
348
});
410
349
}
411
350
412
- private void onping () {
413
- this .lastPing = new Date ();
414
- this .emitAll (EVENT_PING );
415
- }
416
-
417
- private void onpong () {
418
- this .emitAll (EVENT_PONG ,
419
- null != this .lastPing ? new Date ().getTime () - this .lastPing .getTime () : 0 );
420
- }
421
-
422
351
private void ondata (String data ) {
423
- this .decoder .add (data );
352
+ try {
353
+ this .decoder .add (data );
354
+ } catch (DecodingException e ) {
355
+ this .onerror (e );
356
+ }
424
357
}
425
358
426
359
private void ondata (byte [] data ) {
427
- this .decoder .add (data );
360
+ try {
361
+ this .decoder .add (data );
362
+ } catch (DecodingException e ) {
363
+ this .onerror (e );
364
+ }
428
365
}
429
366
430
367
private void ondecoded (Packet packet ) {
@@ -433,7 +370,7 @@ private void ondecoded(Packet packet) {
433
370
434
371
private void onerror (Exception err ) {
435
372
logger .log (Level .FINE , "error" , err );
436
- this .emitAll (EVENT_ERROR , err );
373
+ this .emit (EVENT_ERROR , err );
437
374
}
438
375
439
376
/**
@@ -444,41 +381,31 @@ private void onerror(Exception err) {
444
381
* @return a socket instance for the namespace.
445
382
*/
446
383
public Socket socket (final String nsp , Options opts ) {
447
- Socket socket = this .nsps .get (nsp );
448
- if (socket == null ) {
449
- socket = new Socket (this , nsp , opts );
450
- Socket _socket = this .nsps .putIfAbsent (nsp , socket );
451
- if (_socket != null ) {
452
- socket = _socket ;
453
- } else {
454
- final Manager self = this ;
455
- final Socket s = socket ;
456
- socket .on (Socket .EVENT_CONNECTING , new Listener () {
457
- @ Override
458
- public void call (Object ... args ) {
459
- self .connecting .add (s );
460
- }
461
- });
462
- socket .on (Socket .EVENT_CONNECT , new Listener () {
463
- @ Override
464
- public void call (Object ... objects ) {
465
- s .id = self .generateId (nsp );
466
- }
467
- });
384
+ synchronized (this .nsps ) {
385
+ Socket socket = this .nsps .get (nsp );
386
+ if (socket == null ) {
387
+ socket = new Socket (this , nsp , opts );
388
+ this .nsps .put (nsp , socket );
468
389
}
390
+ return socket ;
469
391
}
470
- return socket ;
471
392
}
472
393
473
394
public Socket socket (String nsp ) {
474
395
return socket (nsp , null );
475
396
}
476
397
477
- /*package*/ void destroy (Socket socket ) {
478
- this .connecting .remove (socket );
479
- if (!this .connecting .isEmpty ()) return ;
398
+ /*package*/ void destroy () {
399
+ synchronized (this .nsps ) {
400
+ for (Socket socket : this .nsps .values ()) {
401
+ if (socket .isActive ()) {
402
+ logger .fine ("socket is still active, skipping close" );
403
+ return ;
404
+ }
405
+ }
480
406
481
- this .close ();
407
+ this .close ();
408
+ }
482
409
}
483
410
484
411
/*package*/ void packet (Packet packet ) {
@@ -487,10 +414,6 @@ public Socket socket(String nsp) {
487
414
}
488
415
final Manager self = this ;
489
416
490
- if (packet .query != null && !packet .query .isEmpty () && packet .type == Parser .CONNECT ) {
491
- packet .nsp += "?" + packet .query ;
492
- }
493
-
494
417
if (!self .encoding ) {
495
418
self .encoding = true ;
496
419
this .encoder .encode (packet , new Parser .Encoder .Callback () {
@@ -528,7 +451,6 @@ private void cleanup() {
528
451
529
452
this .packetBuffer .clear ();
530
453
this .encoding = false ;
531
- this .lastPing = null ;
532
454
533
455
this .decoder .destroy ();
534
456
}
@@ -569,7 +491,7 @@ private void reconnect() {
569
491
if (this .backoff .getAttempts () >= this ._reconnectionAttempts ) {
570
492
logger .fine ("reconnect failed" );
571
493
this .backoff .reset ();
572
- this .emitAll (EVENT_RECONNECT_FAILED );
494
+ this .emit (EVENT_RECONNECT_FAILED );
573
495
this .reconnecting = false ;
574
496
} else {
575
497
long delay = this .backoff .duration ();
@@ -587,8 +509,7 @@ public void run() {
587
509
588
510
logger .fine ("attempting reconnect" );
589
511
int attempts = self .backoff .getAttempts ();
590
- self .emitAll (EVENT_RECONNECT_ATTEMPT , attempts );
591
- self .emitAll (EVENT_RECONNECTING , attempts );
512
+ self .emit (EVENT_RECONNECT_ATTEMPT , attempts );
592
513
593
514
// check again for the case socket closed in above events
594
515
if (self .skipReconnect ) return ;
@@ -600,7 +521,7 @@ public void call(Exception err) {
600
521
logger .fine ("reconnect attempt error" );
601
522
self .reconnecting = false ;
602
523
self .reconnect ();
603
- self .emitAll (EVENT_RECONNECT_ERROR , err );
524
+ self .emit (EVENT_RECONNECT_ERROR , err );
604
525
} else {
605
526
logger .fine ("reconnect success" );
606
527
self .onreconnect ();
@@ -625,8 +546,7 @@ private void onreconnect() {
625
546
int attempts = this .backoff .getAttempts ();
626
547
this .reconnecting = false ;
627
548
this .backoff .reset ();
628
- this .updateSocketIds ();
629
- this .emitAll (EVENT_RECONNECT , attempts );
549
+ this .emit (EVENT_RECONNECT , attempts );
630
550
}
631
551
632
552
@@ -652,6 +572,7 @@ public static class Options extends io.socket.engineio.client.Socket.Options {
652
572
public double randomizationFactor ;
653
573
public Parser .Encoder encoder ;
654
574
public Parser .Decoder decoder ;
575
+ public Map <String , String > auth ;
655
576
656
577
/**
657
578
* Connection timeout (ms). Set -1 to disable.
0 commit comments