Skip to content

Commit 78f1076

Browse files
author
Ben Kay
committed
Move handshake to its own thread.
1 parent 1cb62d5 commit 78f1076

File tree

2 files changed

+69
-48
lines changed

2 files changed

+69
-48
lines changed

src/com/clwillingham/socket/io/IOSocket.java

+68-47
Original file line numberDiff line numberDiff line change
@@ -23,8 +23,9 @@ public class IOSocket {
2323
private int closingTimeout;
2424
private int connectTimeout = 10000;
2525
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;
2829
private Timer timer;
2930

3031
private int ackCount = 0;
@@ -35,60 +36,29 @@ public class IOSocket {
3536
private boolean open;
3637

3738
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+
3950
this.callback = callback;
4051
}
4152

42-
public void connect() throws IOException {
53+
public void connect() {
4354
synchronized(this) {
4455
connecting = true;
4556
}
4657

4758
timer = new Timer();
4859
timer.schedule(new ConnectTimeout(), connectTimeout);
4960

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();
9262
}
9363

9464
public void emit(String event, JSONObject... message) throws IOException {
@@ -149,6 +119,7 @@ synchronized void onClose() {
149119
}
150120

151121
synchronized void onConnect() {
122+
152123
if (!connected) {
153124
connected = true;
154125
connecting = false;
@@ -179,6 +150,13 @@ synchronized void onDisconnect() {
179150
//TODO: reconnect
180151
}
181152
}
153+
154+
private synchronized void onConnectFailure() {
155+
connecting = false;
156+
connected = false;
157+
158+
callback.onConnectFailure();
159+
}
182160

183161
public void onAcknowledge(int ackId, JSONArray data) {
184162
AckCallback ackCallback = ackCallbacks.get(ackId);
@@ -268,6 +246,49 @@ public void setProtocals(String[] protocals) {
268246
public String[] getProtocals() {
269247
return protocals;
270248
}
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+
}
271292

272293
private class ConnectTimeout extends TimerTask {
273294
@Override
@@ -286,7 +307,7 @@ public void run() {
286307
e.printStackTrace();
287308
}
288309
}
289-
callback.onConnectFailure();
310+
onConnectFailure();
290311
}
291312
}
292313
}

src/com/clwillingham/socket/io/IOWebSocket.java

+1-1
Original file line numberDiff line numberDiff line change
@@ -185,5 +185,5 @@ public void run() {
185185
}
186186
ioSocket.onDisconnect();
187187
}
188-
};
188+
}
189189
}

0 commit comments

Comments
 (0)