@@ -23,8 +23,9 @@ public class IOSocket {
23
23
private int closingTimeout ;
24
24
private int connectTimeout = 10000 ;
25
25
private String [] protocals ;
26
- private String webSocketAddress ;
27
- private MessageCallback callback ;
26
+ private final String webSocketAddress ;
27
+ private final String namespace ;
28
+ private final MessageCallback callback ;
28
29
private Timer timer ;
29
30
30
31
private int ackCount = 0 ;
@@ -35,60 +36,29 @@ public class IOSocket {
35
36
private boolean open ;
36
37
37
38
public IOSocket (String address , MessageCallback callback ){
38
- webSocketAddress = address ;
39
+
40
+ // check for socket.io namespace
41
+ int i = address .lastIndexOf ("/" );
42
+ if (address .charAt (i -1 ) != '/' ) {
43
+ namespace = address .substring (i );
44
+ webSocketAddress = address .substring (0 , i );
45
+ } else {
46
+ namespace = "" ;
47
+ webSocketAddress = address ;
48
+ }
49
+
39
50
this .callback = callback ;
40
51
}
41
52
42
- public void connect () throws IOException {
53
+ public void connect () {
43
54
synchronized (this ) {
44
55
connecting = true ;
45
56
}
46
57
47
58
timer = new Timer ();
48
59
timer .schedule (new ConnectTimeout (), connectTimeout );
49
60
50
- // check for socket.io namespace
51
- String namespace = "" ;
52
- int i = webSocketAddress .lastIndexOf ("/" );
53
- if (webSocketAddress .charAt (i -1 ) != '/' ) {
54
- namespace = webSocketAddress .substring (i );
55
- webSocketAddress = webSocketAddress .substring (0 , i );
56
- }
57
-
58
- // perform handshake
59
- try {
60
- String address = webSocketAddress .replace ("ws://" , "http://" );
61
- URL url = new URL (address + "/socket.io/1/" ); //handshake url
62
- URLConnection connection = url .openConnection ();
63
- connection .setConnectTimeout (connectTimeout );
64
- connection .setReadTimeout (connectTimeout );
65
- InputStream stream = connection .getInputStream ();
66
- Scanner in = new Scanner (stream );
67
- String response = in .nextLine (); //pull the response
68
- System .out .println (response );
69
-
70
- // process handshake response
71
- // example: 4d4f185e96a7b:15:10:websocket,xhr-polling
72
- if (response .contains (":" )) {
73
- String [] data = response .split (":" );
74
- setSessionID (data [0 ]);
75
- setHeartTimeout (Integer .parseInt (data [1 ]) * 1000 );
76
- setClosingTimeout (Integer .parseInt (data [2 ]) * 1000 );
77
- setProtocals (data [3 ].split ("," ));
78
- }
79
-
80
- webSocket = new IOWebSocket (URI .create (webSocketAddress +"/socket.io/1/websocket/" +sessionID ), this , callback );
81
- webSocket .setNamespace (namespace );
82
- webSocket .connect ();
83
- } catch (IOException e ) {
84
- synchronized (this ) {
85
- if (connecting ) {
86
- connecting = false ;
87
- }
88
- }
89
-
90
- throw new IOException (e );
91
- }
61
+ (new Thread (new Handshake ())).start ();
92
62
}
93
63
94
64
public void emit (String event , JSONObject ... message ) throws IOException {
@@ -149,6 +119,7 @@ synchronized void onClose() {
149
119
}
150
120
151
121
synchronized void onConnect () {
122
+
152
123
if (!connected ) {
153
124
connected = true ;
154
125
connecting = false ;
@@ -179,6 +150,13 @@ synchronized void onDisconnect() {
179
150
//TODO: reconnect
180
151
}
181
152
}
153
+
154
+ private synchronized void onConnectFailure () {
155
+ connecting = false ;
156
+ connected = false ;
157
+
158
+ callback .onConnectFailure ();
159
+ }
182
160
183
161
public void onAcknowledge (int ackId , JSONArray data ) {
184
162
AckCallback ackCallback = ackCallbacks .get (ackId );
@@ -268,6 +246,49 @@ public void setProtocals(String[] protocals) {
268
246
public String [] getProtocals () {
269
247
return protocals ;
270
248
}
249
+
250
+ private synchronized void onHandshakeSuccess () {
251
+ if (!connecting ) {
252
+ return ;
253
+ }
254
+
255
+ webSocket = new IOWebSocket (URI .create (webSocketAddress +"/socket.io/1/websocket/" +sessionID ), this , callback );
256
+ webSocket .setNamespace (namespace );
257
+ webSocket .connect ();
258
+ }
259
+
260
+ private class Handshake implements Runnable {
261
+ @ Override
262
+ public void run () {
263
+ try {
264
+ String address = webSocketAddress .replace ("ws://" , "http://" );
265
+ URL url = new URL (address + "/socket.io/1/" ); //handshake url
266
+ URLConnection connection = url .openConnection ();
267
+ connection .setConnectTimeout (connectTimeout );
268
+ connection .setReadTimeout (connectTimeout );
269
+ InputStream stream = connection .getInputStream ();
270
+ Scanner in = new Scanner (stream );
271
+ String response = in .nextLine (); //pull the response
272
+
273
+ // process handshake response
274
+ // example: 4d4f185e96a7b:15:10:websocket,xhr-polling
275
+ if (response .contains (":" )) {
276
+ String [] data = response .split (":" );
277
+ setSessionID (data [0 ]);
278
+ setHeartTimeout (Integer .parseInt (data [1 ]) * 1000 );
279
+ setClosingTimeout (Integer .parseInt (data [2 ]) * 1000 );
280
+ setProtocals (data [3 ].split ("," ));
281
+
282
+ onHandshakeSuccess ();
283
+ } else {
284
+ onConnectFailure ();
285
+ }
286
+
287
+ } catch (IOException e ) {
288
+ onConnectFailure ();
289
+ }
290
+ }
291
+ }
271
292
272
293
private class ConnectTimeout extends TimerTask {
273
294
@ Override
@@ -286,7 +307,7 @@ public void run() {
286
307
e .printStackTrace ();
287
308
}
288
309
}
289
- callback . onConnectFailure ();
310
+ onConnectFailure ();
290
311
}
291
312
}
292
313
}
0 commit comments