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
@@ -201,17 +201,17 @@ public void run() {
201
201
connectTransport ();
202
202
}
203
203
204
-
205
204
};
206
205
207
206
/**
208
207
* Set the socket factory used for SSL connections.
208
+ *
209
209
* @param socketFactory
210
210
*/
211
211
public static void setDefaultSSLSocketFactory (SSLSocketFactory socketFactory ) {
212
212
sslSocketFactory = socketFactory ;
213
213
}
214
-
214
+
215
215
/**
216
216
* Creates a new connection or returns the corresponding one.
217
217
*
@@ -269,7 +269,7 @@ public void unregister(SocketIO socket) {
269
269
sendPlain ("0::" + socket .getNamespace ());
270
270
sockets .remove (socket .getNamespace ());
271
271
socket .getCallback ().onDisconnect ();
272
-
272
+
273
273
if (sockets .size () == 0 ) {
274
274
cleanup ();
275
275
}
@@ -287,18 +287,19 @@ private void handshake() {
287
287
setState (STATE_HANDSHAKE );
288
288
url = new URL (IOConnection .this .url .toString () + SOCKET_IO_1 );
289
289
connection = url .openConnection ();
290
- if (connection instanceof HttpsURLConnection ) {
291
- ((HttpsURLConnection )connection ).setSSLSocketFactory (sslSocketFactory );
290
+ if (connection instanceof HttpsURLConnection ) {
291
+ ((HttpsURLConnection ) connection )
292
+ .setSSLSocketFactory (sslSocketFactory );
292
293
}
293
294
connection .setConnectTimeout (connectTimeout );
294
295
connection .setReadTimeout (connectTimeout );
295
-
296
+
296
297
/* Setting the request headers */
297
298
for (Entry <Object , Object > entry : headers .entrySet ()) {
298
299
connection .setRequestProperty ((String ) entry .getKey (),
299
300
(String ) entry .getValue ());
300
301
}
301
-
302
+
302
303
InputStream stream = connection .getInputStream ();
303
304
Scanner in = new Scanner (stream );
304
305
response = in .nextLine ();
@@ -487,15 +488,15 @@ private void resetTimeout() {
487
488
* @param message
488
489
* the message
489
490
* @return the iO callback
490
- * @throws SocketIOException
491
+ * @throws SocketIOException
491
492
*/
492
493
private IOCallback findCallback (IOMessage message ) throws SocketIOException {
493
- if ("" .equals (message .getEndpoint ()))
494
+ if ("" .equals (message .getEndpoint ()))
494
495
return this ;
495
496
SocketIO socket = sockets .get (message .getEndpoint ());
496
497
if (socket == null ) {
497
- throw new SocketIOException ("Cannot find socket for '" + message . getEndpoint ()
498
- + "'" );
498
+ throw new SocketIOException ("Cannot find socket for '"
499
+ + message . getEndpoint () + "'" );
499
500
}
500
501
return socket .getCallback ();
501
502
}
@@ -564,28 +565,31 @@ public void transportError(Exception error) {
564
565
}
565
566
566
567
/**
567
- * {@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
568
570
*
569
571
* @param text
570
572
* the text
571
573
*/
572
574
public void transportData (String text ) {
573
- if (!text .startsWith (FRAME_DELIMITER )) {
575
+ if (!text .startsWith (FRAME_DELIMITER )) {
574
576
transportMessage (text );
575
577
return ;
576
578
}
577
-
578
- 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 );
579
582
while (fragments .hasNext ()) {
580
583
int length = Integer .parseInt (fragments .next ());
581
584
String string = (String ) fragments .next ();
582
- // Potential BUG: it is not defined if length is in bytes or characters. Assuming characters.
583
-
584
- 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 ()) {
585
589
error (new SocketIOException ("Garbage from server: " + text ));
586
590
return ;
587
591
}
588
-
592
+
589
593
transportMessage (string );
590
594
}
591
595
}
@@ -678,8 +682,7 @@ public void transportMessage(String text) {
678
682
if (args .isNull (i ) == false )
679
683
argsArray [i ] = args .get (i );
680
684
}
681
- }
682
- else
685
+ } else
683
686
argsArray = new Object [0 ];
684
687
String eventName = event .getString ("name" );
685
688
try {
@@ -869,38 +872,38 @@ public IOTransport getTransport() {
869
872
@ Override
870
873
public void onDisconnect () {
871
874
SocketIO socket = sockets .get ("" );
872
- if (socket != null )
875
+ if (socket != null )
873
876
socket .getCallback ().onConnect ();
874
877
}
875
878
876
879
@ Override
877
880
public void onConnect () {
878
881
SocketIO socket = sockets .get ("" );
879
- if (socket != null )
882
+ if (socket != null )
880
883
socket .getCallback ().onConnect ();
881
884
}
882
885
883
886
@ Override
884
887
public void onMessage (String data , IOAcknowledge ack ) {
885
- for (SocketIO socket : sockets .values ())
888
+ for (SocketIO socket : sockets .values ())
886
889
socket .getCallback ().onMessage (data , ack );
887
890
}
888
891
889
892
@ Override
890
893
public void onMessage (JSONObject json , IOAcknowledge ack ) {
891
- for (SocketIO socket : sockets .values ())
894
+ for (SocketIO socket : sockets .values ())
892
895
socket .getCallback ().onMessage (json , ack );
893
896
}
894
897
895
898
@ Override
896
899
public void on (String event , IOAcknowledge ack , Object ... args ) {
897
- for (SocketIO socket : sockets .values ())
900
+ for (SocketIO socket : sockets .values ())
898
901
socket .getCallback ().on (event , ack , args );
899
902
}
900
903
901
904
@ Override
902
905
public void onError (SocketIOException socketIOException ) {
903
- for (SocketIO socket : sockets .values ())
906
+ for (SocketIO socket : sockets .values ())
904
907
socket .getCallback ().onError (socketIOException );
905
908
}
906
909
}
0 commit comments