Skip to content

Commit 74839db

Browse files
committed
integrated websocket and porting fc80d8e
1 parent 18141f0 commit 74839db

File tree

6 files changed

+65
-88
lines changed

6 files changed

+65
-88
lines changed

pom.xml

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -29,9 +29,9 @@
2929
</dependency>
3030

3131
<dependency>
32-
<groupId>de.roderick</groupId>
33-
<artifactId>weberknecht</artifactId>
34-
<version>0.1.1</version>
32+
<groupId>org.java_websocket</groupId>
33+
<artifactId>WebSocket</artifactId>
34+
<version>1.0.0-SNAPSHOT</version>
3535
</dependency>
3636

3737
<dependency>

src/main/java/io/socket/IOConnection.java

Lines changed: 27 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@
2727
import java.util.logging.Logger;
2828

2929
import javax.net.ssl.HttpsURLConnection;
30-
import javax.net.ssl.SSLSocketFactory;
30+
import javax.net.ssl.SSLContext;
3131

3232
import com.google.gson.JsonArray;
3333
import com.google.gson.JsonElement;
@@ -70,8 +70,7 @@ class IOConnection implements IOCallback {
7070
public static final String SOCKET_IO_1 = "/socket.io/1/";
7171

7272
/** The SSL socket factory for HTTPS connections */
73-
private static SSLSocketFactory sslSocketFactory = (SSLSocketFactory) SSLSocketFactory
74-
.getDefault();
73+
private static SSLContext sslContext = null;
7574

7675
/** All available connections. */
7776
private static HashMap<String, List<IOConnection>> connections = new HashMap<String, List<IOConnection>>();
@@ -209,10 +208,19 @@ public void run() {
209208
/**
210209
* Set the socket factory used for SSL connections.
211210
*
212-
* @param socketFactory
211+
* @param sslContext
213212
*/
214-
public static void setDefaultSSLSocketFactory(SSLSocketFactory socketFactory) {
215-
sslSocketFactory = socketFactory;
213+
public static void setSslContext(SSLContext sslContext) {
214+
IOConnection.sslContext = sslContext;
215+
}
216+
217+
/**
218+
* Get the socket factory used for SSL connections.
219+
*
220+
* @return socketFactory
221+
*/
222+
public static SSLContext getSslContext() {
223+
return sslContext;
216224
}
217225

218226
/**
@@ -294,7 +302,7 @@ private void handshake() {
294302
connection = url.openConnection();
295303
if (connection instanceof HttpsURLConnection) {
296304
((HttpsURLConnection) connection)
297-
.setSSLSocketFactory(sslSocketFactory);
305+
.setSSLSocketFactory(sslContext.getSocketFactory());
298306
}
299307
connection.setConnectTimeout(connectTimeout);
300308
connection.setReadTimeout(connectTimeout);
@@ -355,17 +363,20 @@ else if (_id.endsWith("+") == false)
355363
final String id = _id;
356364
final String endPoint = message.getEndpoint();
357365
return new IOAcknowledge() {
366+
@Override
358367
public void ack(JsonElement... args) {
359368
JsonArray array = new JsonArray();
360369
for (JsonElement o : args) {
361370
try {
362371
array.add(o);
363-
// array.put(o == null ? JSONObject.NULL : o);
364372
} catch (Exception e) {
365-
error(new SocketIOException( "You can only put values in IOAcknowledge.ack() which can be handled by JSONArray.put()", e));
373+
error(new SocketIOException(
374+
"You can only put values in IOAcknowledge.ack() which can be handled by JSONArray.put()",
375+
e));
366376
}
367377
}
368-
IOMessage ackMsg = new IOMessage(IOMessage.TYPE_ACK, endPoint, id + array.toString());
378+
IOMessage ackMsg = new IOMessage(IOMessage.TYPE_ACK, endPoint,
379+
id + array.toString());
369380
sendPlain(ackMsg.toString());
370381
}
371382
};
@@ -686,7 +697,8 @@ public void transportMessage(String text) {
686697
argsArray = new JsonElement[0];
687698
String eventName = event.get("name").getAsString();
688699
try {
689-
findCallback(message).on(eventName, remoteAcknowledge(message), argsArray);
700+
findCallback(message).on(eventName,
701+
remoteAcknowledge(message), argsArray);
690702
} catch (Exception e) {
691703
error(new SocketIOException(
692704
"Exception was thrown in on(String, JSONObject[]).\n"
@@ -724,7 +736,8 @@ public void transportMessage(String text) {
724736
break;
725737
case IOMessage.TYPE_ERROR:
726738
try {
727-
findCallback(message).onError( new SocketIOException(message.getData()));
739+
findCallback(message).onError(
740+
new SocketIOException(message.getData()));
728741
} catch (SocketIOException e) {
729742
error(e);
730743
}
@@ -824,6 +837,7 @@ public void emit(SocketIO socket, String event, IOAcknowledge ack, Object... arg
824837
}
825838
}
826839

840+
827841
JsonObject jobj = new JsonObject();
828842
jobj.add("name", new JsonPrimitive(event));
829843
jobj.add("args", jarray);
@@ -833,7 +847,7 @@ public void emit(SocketIO socket, String event, IOAcknowledge ack, Object... arg
833847
sendPlain(message.toString());
834848

835849
} catch (JsonParseException e) {
836-
error(new SocketIOException("Error while emitting an event. Make sure you only try to send arguments, which can be serialized into JSON.") );
850+
error(new SocketIOException("Error while emitting an event. Make sure you only try to send arguments, which can be serialized into JSON."));
837851
}
838852

839853
}

src/main/java/io/socket/SocketIO.java

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@
1212
import java.net.URL;
1313
import java.util.Properties;
1414

15-
import javax.net.ssl.SSLSocketFactory;
15+
import javax.net.ssl.SSLContext;
1616

1717
import com.google.gson.JsonObject;
1818

@@ -127,8 +127,8 @@ public SocketIO(final URL url) {
127127
* Set the socket factory used for SSL connections.
128128
* @param socketFactory
129129
*/
130-
public static void setDefaultSSLSocketFactory(SSLSocketFactory socketFactory) {
131-
IOConnection.setDefaultSSLSocketFactory(socketFactory);
130+
public static void setDefaultSSLSocketFactory(SSLContext sslContext) {
131+
IOConnection.setSslContext(sslContext);
132132
}
133133

134134
/**
@@ -339,7 +339,7 @@ public void reconnect() {
339339
* not connected or currently connecting
340340
*/
341341
public boolean isConnected() {
342-
return this.connection.isConnected();
342+
return this.connection != null && this.connection.isConnected();
343343
}
344344

345345
/**
Lines changed: 23 additions & 67 deletions
Original file line numberDiff line numberDiff line change
@@ -1,72 +1,36 @@
1-
/*
2-
* socket.io-java-client WebsocketTransport.java
3-
*
4-
* Copyright (c) 2012, Enno Boland
5-
* socket.io-java-client is a implementation of the socket.io protocol in Java.
6-
*
7-
* See LICENSE file for more information
8-
*/
91
package io.socket;
102

11-
123
import java.io.IOException;
134
import java.net.URI;
145
import java.net.URL;
156
import java.util.regex.Pattern;
167

17-
import de.roderick.weberknecht.WebSocketConnection;
18-
import de.roderick.weberknecht.WebSocketEventHandler;
19-
import de.roderick.weberknecht.WebSocketException;
20-
import de.roderick.weberknecht.WebSocketMessage;
8+
import javax.net.ssl.SSLContext;
219

22-
/**
23-
* The Class WebsocketTransport.
24-
*/
25-
class WebsocketTransport implements IOTransport, WebSocketEventHandler {
26-
27-
WebSocketConnection websocket;
10+
import org.java_websocket.client.DefaultSSLWebSocketClientFactory;
11+
import org.java_websocket.client.WebSocketClient;
12+
import org.java_websocket.handshake.ServerHandshake;
13+
14+
class WebsocketTransport extends WebSocketClient implements IOTransport {
2815

29-
/** Pattern used to replace http:// by ws:// respectively https:// by wss:// */
3016
private final static Pattern PATTERN_HTTP = Pattern.compile("^http");
31-
32-
/** The String to identify this Transport */
3317
public static final String TRANSPORT_NAME = "websocket";
3418

35-
/** The IOConnection of this transport. */
3619
private IOConnection connection;
3720

38-
/**
39-
* Creates a new Transport for the given url an {@link IOConnection}.
40-
*
41-
* @param url the url
42-
* @param connection the connection
43-
* @return the iO transport
44-
*/
4521
public static IOTransport create(URL url, IOConnection connection) {
46-
URI uri = URI.create(
47-
PATTERN_HTTP.matcher(url.toString()).replaceFirst("ws")
48-
+ IOConnection.SOCKET_IO_1 + TRANSPORT_NAME
49-
+ "/" + connection.getSessionId());
22+
URI uri = URI.create( PATTERN_HTTP.matcher(url.toString()).replaceFirst("ws") + IOConnection.SOCKET_IO_1 + TRANSPORT_NAME + "/" + connection.getSessionId());
5023

5124
return new WebsocketTransport(uri, connection);
5225
}
53-
54-
/**
55-
* Instantiates a new websocket transport.
56-
*
57-
* @param uri the uri
58-
* @param connection the connection
59-
* @throws WebSocketException
60-
*/
26+
6127
public WebsocketTransport(URI uri, IOConnection connection) {
62-
try {
63-
websocket = new WebSocketConnection(uri);
64-
} catch (WebSocketException e) {
65-
connection.transportError(e);
66-
return;
67-
}
28+
super(uri);
6829
this.connection = connection;
69-
websocket.setEventHandler(this);
30+
SSLContext context = IOConnection.getSslContext();
31+
if("wss".equals(uri.getScheme()) && context != null) {
32+
this.setWebSocketFactory(new DefaultSSLWebSocketClientFactory(context));
33+
}
7034
}
7135

7236
/* (non-Javadoc)
@@ -75,7 +39,7 @@ public WebsocketTransport(URI uri, IOConnection connection) {
7539
@Override
7640
public void disconnect() {
7741
try {
78-
websocket.close();
42+
this.close();
7943
} catch (Exception e) {
8044
connection.transportError(e);
8145
}
@@ -106,39 +70,31 @@ public void invalidate() {
10670
}
10771

10872
@Override
109-
public void onClose() {
73+
public void onClose(int code, String reason, boolean remote) {
11074
if(connection != null)
11175
connection.transportDisconnected();
11276
}
11377

11478
@Override
115-
public void onMessage(WebSocketMessage arg0) {
79+
public void onMessage(String text) {
11680
if(connection != null)
117-
connection.transportMessage(arg0.getText());
81+
connection.transportMessage(text);
11882
}
11983

12084
@Override
121-
public void onOpen() {
85+
public void onOpen(ServerHandshake handshakedata) {
12286
if(connection != null)
12387
connection.transportConnected();
12488
}
12589

12690
@Override
127-
public void connect() {
128-
try {
129-
websocket.connect();
130-
} catch (Exception e) {
131-
connection.transportError(e);
132-
}
91+
public String getName() {
92+
return TRANSPORT_NAME;
13393
}
13494

13595
@Override
136-
public void send(String text) throws Exception {
137-
websocket.send(text);
138-
}
96+
public void onError(Exception ex) {
97+
// TODO Auto-generated method stub
13998

140-
@Override
141-
public String getName() {
142-
return TRANSPORT_NAME;
14399
}
144-
}
100+
}

src/main/java/io/socket/XhrTransport.java

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,9 @@
2020
import java.util.Iterator;
2121
import java.util.concurrent.ConcurrentLinkedQueue;
2222

23+
import javax.net.ssl.HttpsURLConnection;
24+
import javax.net.ssl.SSLContext;
25+
2326
/**
2427
* The Class XhrTransport.
2528
*/
@@ -76,6 +79,10 @@ public void run() {
7679
URL url = new URL(XhrTransport.this.url.toString() + "?t="
7780
+ System.currentTimeMillis());
7881
urlConnection = (HttpURLConnection) url.openConnection();
82+
SSLContext context = IOConnection.getSslContext();
83+
if(urlConnection instanceof HttpsURLConnection && context != null) {
84+
((HttpsURLConnection)urlConnection).setSSLSocketFactory(context.getSocketFactory());
85+
}
7986
if (!queue.isEmpty()) {
8087
urlConnection.setDoOutput(true);
8188
OutputStream output = urlConnection.getOutputStream();

src/test/java/io/socket/AbstractTestSocketIO.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ public abstract class AbstractTestSocketIO implements IOCallback {
4040
private static final String REQUEST_ACKNOWLEDGE = "requestAcknowledge";
4141

4242
/** The Constant to the node executable */
43-
private final static String NODE = "node";
43+
private final static String NODE = "/usr/local/bin/node";
4444

4545
/** The port of this test, randomly choosed */
4646
private int port = -1;

0 commit comments

Comments
 (0)