25
25
import java .util .TimerTask ;
26
26
import java .util .concurrent .ConcurrentLinkedQueue ;
27
27
import java .util .logging .Logger ;
28
-
29
28
import javax .net .ssl .HttpsURLConnection ;
30
29
import javax .net .ssl .SSLSocketFactory ;
31
30
39
38
class IOConnection implements IOCallback {
40
39
/** Debug logger */
41
40
static final Logger logger = Logger .getLogger ("io.socket" );
42
-
41
+
43
42
public static final String FRAME_DELIMITER = "\ufffd " ;
44
43
45
44
/** The Constant STATE_INIT. */
@@ -67,8 +66,9 @@ class IOConnection implements IOCallback {
67
66
public static final String SOCKET_IO_1 = "/socket.io/1/" ;
68
67
69
68
/** The SSL socket factory for HTTPS connections */
70
- private static SSLSocketFactory sslSocketFactory = (SSLSocketFactory )SSLSocketFactory .getDefault ();
71
-
69
+ private static SSLSocketFactory sslSocketFactory = (SSLSocketFactory ) SSLSocketFactory
70
+ .getDefault ();
71
+
72
72
/** All available connections. */
73
73
private static HashMap <String , List <IOConnection >> connections = new HashMap <String , List <IOConnection >>();
74
74
@@ -177,23 +177,6 @@ public void run() {
177
177
}
178
178
}
179
179
}
180
-
181
- private IOReconnectScheduler reconnectScheduler = new IOReconnectScheduler () {
182
-
183
- @ Override
184
- public void scheduleReconnect (Timer timer , TimerTask task ) {
185
- timer .schedule (task , 1000 );
186
- }
187
-
188
- @ Override
189
- public void onReconnect () {
190
- }
191
-
192
- };
193
-
194
- public void setReconnectScheduler (IOReconnectScheduler scheduler ) {
195
- reconnectScheduler = scheduler ;
196
- }
197
180
198
181
/**
199
182
* The Class ConnectThread. Handles connecting to the server with an
@@ -218,17 +201,17 @@ public void run() {
218
201
connectTransport ();
219
202
}
220
203
221
-
222
204
};
223
205
224
206
/**
225
207
* Set the socket factory used for SSL connections.
208
+ *
226
209
* @param socketFactory
227
210
*/
228
211
public static void setDefaultSSLSocketFactory (SSLSocketFactory socketFactory ) {
229
212
sslSocketFactory = socketFactory ;
230
213
}
231
-
214
+
232
215
/**
233
216
* Creates a new connection or returns the corresponding one.
234
217
*
@@ -286,7 +269,7 @@ public void unregister(SocketIO socket) {
286
269
sendPlain ("0::" + socket .getNamespace ());
287
270
sockets .remove (socket .getNamespace ());
288
271
socket .getCallback ().onDisconnect ();
289
-
272
+
290
273
if (sockets .size () == 0 ) {
291
274
cleanup ();
292
275
}
@@ -304,18 +287,19 @@ private void handshake() {
304
287
setState (STATE_HANDSHAKE );
305
288
url = new URL (IOConnection .this .url .toString () + SOCKET_IO_1 );
306
289
connection = url .openConnection ();
307
- if (connection instanceof HttpsURLConnection ) {
308
- ((HttpsURLConnection )connection ).setSSLSocketFactory (sslSocketFactory );
290
+ if (connection instanceof HttpsURLConnection ) {
291
+ ((HttpsURLConnection ) connection )
292
+ .setSSLSocketFactory (sslSocketFactory );
309
293
}
310
294
connection .setConnectTimeout (connectTimeout );
311
295
connection .setReadTimeout (connectTimeout );
312
-
296
+
313
297
/* Setting the request headers */
314
298
for (Entry <Object , Object > entry : headers .entrySet ()) {
315
299
connection .setRequestProperty ((String ) entry .getKey (),
316
300
(String ) entry .getValue ());
317
301
}
318
-
302
+
319
303
InputStream stream = connection .getInputStream ();
320
304
Scanner in = new Scanner (stream );
321
305
response = in .nextLine ();
@@ -432,7 +416,7 @@ private void cleanup() {
432
416
sockets .clear ();
433
417
synchronized (connections ) {
434
418
List <IOConnection > con = connections .get (urlStr );
435
- if (con != null && con .size () > 1 )
419
+ if (con .size () > 1 )
436
420
con .remove (this );
437
421
else
438
422
connections .remove (urlStr );
@@ -504,15 +488,15 @@ private void resetTimeout() {
504
488
* @param message
505
489
* the message
506
490
* @return the iO callback
507
- * @throws SocketIOException
491
+ * @throws SocketIOException
508
492
*/
509
493
private IOCallback findCallback (IOMessage message ) throws SocketIOException {
510
- if ("" .equals (message .getEndpoint ()))
494
+ if ("" .equals (message .getEndpoint ()))
511
495
return this ;
512
496
SocketIO socket = sockets .get (message .getEndpoint ());
513
497
if (socket == null ) {
514
- throw new SocketIOException ("Cannot find socket for '" + message . getEndpoint ()
515
- + "'" );
498
+ throw new SocketIOException ("Cannot find socket for '"
499
+ + message . getEndpoint () + "'" );
516
500
}
517
501
return socket .getCallback ();
518
502
}
@@ -524,9 +508,7 @@ private IOCallback findCallback(IOMessage message) throws SocketIOException {
524
508
*/
525
509
public void transportConnected () {
526
510
setState (STATE_READY );
527
-
528
- boolean isReconnecting = (reconnectTask != null );
529
- if (isReconnecting ) {
511
+ if (reconnectTask != null ) {
530
512
reconnectTask .cancel ();
531
513
reconnectTask = null ;
532
514
}
@@ -554,11 +536,7 @@ public void transportConnected() {
554
536
while ((text = outputBuffer .poll ()) != null )
555
537
sendPlain (text );
556
538
}
557
-
558
539
this .keepAliveInQueue = false ;
559
- if (isReconnecting ) {
560
- reconnectScheduler .onReconnect ();
561
- }
562
540
}
563
541
}
564
542
@@ -587,28 +565,31 @@ public void transportError(Exception error) {
587
565
}
588
566
589
567
/**
590
- * {@link IOTransport} should call this function if it does not support framing. If it does, transportMessage should be used
568
+ * {@link IOTransport} should call this function if it does not support
569
+ * framing. If it does, transportMessage should be used
591
570
*
592
571
* @param text
593
572
* the text
594
573
*/
595
574
public void transportData (String text ) {
596
- if (!text .startsWith (FRAME_DELIMITER )) {
575
+ if (!text .startsWith (FRAME_DELIMITER )) {
597
576
transportMessage (text );
598
577
return ;
599
578
}
600
-
601
- Iterator <String > fragments = Arrays .asList (text .split (FRAME_DELIMITER )).listIterator (1 );
579
+
580
+ Iterator <String > fragments = Arrays .asList (text .split (FRAME_DELIMITER ))
581
+ .listIterator (1 );
602
582
while (fragments .hasNext ()) {
603
583
int length = Integer .parseInt (fragments .next ());
604
584
String string = (String ) fragments .next ();
605
- // Potential BUG: it is not defined if length is in bytes or characters. Assuming characters.
606
-
607
- if (length != string .length ()) {
585
+ // Potential BUG: it is not defined if length is in bytes or
586
+ // characters. Assuming characters.
587
+
588
+ if (length != string .length ()) {
608
589
error (new SocketIOException ("Garbage from server: " + text ));
609
590
return ;
610
591
}
611
-
592
+
612
593
transportMessage (string );
613
594
}
614
595
}
@@ -701,8 +682,7 @@ public void transportMessage(String text) {
701
682
if (args .isNull (i ) == false )
702
683
argsArray [i ] = args .get (i );
703
684
}
704
- }
705
- else
685
+ } else
706
686
argsArray = new Object [0 ];
707
687
String eventName = event .getString ("name" );
708
688
try {
@@ -776,7 +756,7 @@ public void reconnect() {
776
756
reconnectTask .cancel ();
777
757
}
778
758
reconnectTask = new ReconnectTask ();
779
- reconnectScheduler . scheduleReconnect ( backgroundTimer , reconnectTask );
759
+ backgroundTimer . schedule ( reconnectTask , 1000 );
780
760
}
781
761
}
782
762
}
@@ -892,38 +872,38 @@ public IOTransport getTransport() {
892
872
@ Override
893
873
public void onDisconnect () {
894
874
SocketIO socket = sockets .get ("" );
895
- if (socket != null )
875
+ if (socket != null )
896
876
socket .getCallback ().onConnect ();
897
877
}
898
878
899
879
@ Override
900
880
public void onConnect () {
901
881
SocketIO socket = sockets .get ("" );
902
- if (socket != null )
882
+ if (socket != null )
903
883
socket .getCallback ().onConnect ();
904
884
}
905
885
906
886
@ Override
907
887
public void onMessage (String data , IOAcknowledge ack ) {
908
- for (SocketIO socket : sockets .values ())
888
+ for (SocketIO socket : sockets .values ())
909
889
socket .getCallback ().onMessage (data , ack );
910
890
}
911
891
912
892
@ Override
913
893
public void onMessage (JSONObject json , IOAcknowledge ack ) {
914
- for (SocketIO socket : sockets .values ())
894
+ for (SocketIO socket : sockets .values ())
915
895
socket .getCallback ().onMessage (json , ack );
916
896
}
917
897
918
898
@ Override
919
899
public void on (String event , IOAcknowledge ack , Object ... args ) {
920
- for (SocketIO socket : sockets .values ())
900
+ for (SocketIO socket : sockets .values ())
921
901
socket .getCallback ().on (event , ack , args );
922
902
}
923
903
924
904
@ Override
925
905
public void onError (SocketIOException socketIOException ) {
926
- for (SocketIO socket : sockets .values ())
906
+ for (SocketIO socket : sockets .values ())
927
907
socket .getCallback ().onError (socketIOException );
928
908
}
929
909
}
0 commit comments