Skip to content
This repository was archived by the owner on Mar 6, 2018. It is now read-only.

Commit 946358c

Browse files
committed
Use Java-Websockets instead of Weberknecht, use SSLContext instead of SSLFactory to make it work with Java-Websockets
1 parent 91ccf13 commit 946358c

File tree

7 files changed

+120
-146
lines changed

7 files changed

+120
-146
lines changed

libs/WebSocket.jar

91.4 KB
Binary file not shown.

libs/weberknecht-0.1.1.jar

-10.6 KB
Binary file not shown.

src/io/socket/IOConnection.java

+15-6
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@
2626
import java.util.concurrent.ConcurrentLinkedQueue;
2727
import java.util.logging.Logger;
2828
import javax.net.ssl.HttpsURLConnection;
29+
import javax.net.ssl.SSLContext;
2930
import javax.net.ssl.SSLSocketFactory;
3031

3132
import org.json.JSONArray;
@@ -66,8 +67,7 @@ class IOConnection implements IOCallback {
6667
public static final String SOCKET_IO_1 = "/socket.io/1/";
6768

6869
/** The SSL socket factory for HTTPS connections */
69-
private static SSLSocketFactory sslSocketFactory = (SSLSocketFactory) SSLSocketFactory
70-
.getDefault();
70+
private static SSLContext sslContext = null;
7171

7272
/** All available connections. */
7373
private static HashMap<String, List<IOConnection>> connections = new HashMap<String, List<IOConnection>>();
@@ -205,10 +205,19 @@ public void run() {
205205
/**
206206
* Set the socket factory used for SSL connections.
207207
*
208-
* @param socketFactory
208+
* @param sslContext
209209
*/
210-
public static void setDefaultSSLSocketFactory(SSLSocketFactory socketFactory) {
211-
sslSocketFactory = socketFactory;
210+
public static void setSslContext(SSLContext sslContext) {
211+
IOConnection.sslContext = sslContext;
212+
}
213+
214+
/**
215+
* Get the socket factory used for SSL connections.
216+
*
217+
* @return socketFactory
218+
*/
219+
public static SSLContext getSslContext() {
220+
return sslContext;
212221
}
213222

214223
/**
@@ -290,7 +299,7 @@ private void handshake() {
290299
connection = url.openConnection();
291300
if (connection instanceof HttpsURLConnection) {
292301
((HttpsURLConnection) connection)
293-
.setSSLSocketFactory(sslSocketFactory);
302+
.setSSLSocketFactory(sslContext.getSocketFactory());
294303
}
295304
connection.setConnectTimeout(connectTimeout);
296305
connection.setReadTimeout(connectTimeout);

src/io/socket/SocketIO.java

+3-2
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
import java.net.URL;
1313
import java.util.Properties;
1414

15+
import javax.net.ssl.SSLContext;
1516
import javax.net.ssl.SSLSocketFactory;
1617

1718
import org.json.JSONObject;
@@ -127,8 +128,8 @@ public SocketIO(final URL url) {
127128
* Set the socket factory used for SSL connections.
128129
* @param socketFactory
129130
*/
130-
public static void setDefaultSSLSocketFactory(SSLSocketFactory socketFactory) {
131-
IOConnection.setDefaultSSLSocketFactory(socketFactory);
131+
public static void setDefaultSSLSocketFactory(SSLContext sslContext) {
132+
IOConnection.setSslContext(sslContext);
132133
}
133134

134135
/**

src/io/socket/WebsocketTransport.java

+94-137
Original file line numberDiff line numberDiff line change
@@ -1,144 +1,101 @@
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;
21-
22-
/**
23-
* The Class WebsocketTransport.
24-
*/
25-
class WebsocketTransport implements IOTransport, WebSocketEventHandler {
26-
27-
WebSocketConnection websocket;
28-
29-
/** Pattern used to replace http:// by ws:// respectively https:// by wss:// */
30-
private final static Pattern PATTERN_HTTP = Pattern.compile("^http");
31-
32-
/** The String to identify this Transport */
33-
public static final String TRANSPORT_NAME = "websocket";
34-
35-
/** The IOConnection of this transport. */
36-
private IOConnection connection;
37-
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-
*/
45-
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());
50-
51-
return new WebsocketTransport(uri, connection);
52-
}
53-
54-
/**
55-
* Instantiates a new websocket transport.
56-
*
57-
* @param uri the uri
58-
* @param connection the connection
59-
* @throws WebSocketException
60-
*/
61-
public WebsocketTransport(URI uri, IOConnection connection) {
62-
try {
63-
websocket = new WebSocketConnection(uri);
64-
} catch (WebSocketException e) {
65-
connection.transportError(e);
66-
return;
67-
}
68-
this.connection = connection;
69-
websocket.setEventHandler(this);
70-
}
71-
72-
/* (non-Javadoc)
73-
* @see io.socket.IOTransport#disconnect()
74-
*/
75-
@Override
76-
public void disconnect() {
77-
try {
78-
websocket.close();
79-
} catch (Exception e) {
80-
connection.transportError(e);
81-
}
82-
}
83-
84-
/* (non-Javadoc)
85-
* @see io.socket.IOTransport#canSendBulk()
86-
*/
87-
@Override
88-
public boolean canSendBulk() {
89-
return false;
90-
}
91-
92-
/* (non-Javadoc)
93-
* @see io.socket.IOTransport#sendBulk(java.lang.String[])
94-
*/
95-
@Override
96-
public void sendBulk(String[] texts) throws IOException {
97-
throw new RuntimeException("Cannot send Bulk!");
98-
}
99-
100-
/* (non-Javadoc)
101-
* @see io.socket.IOTransport#invalidate()
102-
*/
103-
@Override
104-
public void invalidate() {
105-
connection = null;
106-
}
107-
108-
@Override
109-
public void onClose() {
110-
if(connection != null)
111-
connection.transportDisconnected();
112-
}
113-
114-
@Override
115-
public void onMessage(WebSocketMessage arg0) {
116-
if(connection != null)
117-
connection.transportMessage(arg0.getText());
118-
}
119-
120-
@Override
121-
public void onOpen() {
122-
if(connection != null)
123-
connection.transportConnected();
124-
}
125-
126-
@Override
127-
public void connect() {
128-
try {
129-
websocket.connect();
130-
} catch (Exception e) {
131-
connection.transportError(e);
132-
}
133-
}
134-
135-
@Override
136-
public void send(String text) throws Exception {
137-
websocket.send(text);
138-
}
139-
140-
@Override
141-
public String getName() {
142-
return TRANSPORT_NAME;
143-
}
144-
}
8+
import javax.net.ssl.SSLContext;
9+
import javax.net.ssl.SSLSocketFactory;
10+
11+
import org.java_websocket.client.DefaultSSLWebSocketClientFactory;
12+
import org.java_websocket.client.WebSocketClient;
13+
import org.java_websocket.handshake.ServerHandshake;
14+
15+
class WebsocketTransport extends WebSocketClient implements IOTransport {
16+
private final static Pattern PATTERN_HTTP = Pattern.compile("^http");
17+
public static final String TRANSPORT_NAME = "websocket";
18+
private IOConnection connection;
19+
public static IOTransport create(URL url, IOConnection connection) {
20+
URI uri = URI.create(
21+
PATTERN_HTTP.matcher(url.toString()).replaceFirst("ws")
22+
+ IOConnection.SOCKET_IO_1 + TRANSPORT_NAME
23+
+ "/" + connection.getSessionId());
24+
25+
return new WebsocketTransport(uri, connection);
26+
}
27+
28+
public WebsocketTransport(URI uri, IOConnection connection) {
29+
super(uri);
30+
this.connection = connection;
31+
SSLContext context = IOConnection.getSslContext();
32+
if("wss".equals(uri.getScheme()) && context != null) {
33+
this.setWebSocketFactory(new DefaultSSLWebSocketClientFactory(context));
34+
}
35+
}
36+
37+
/* (non-Javadoc)
38+
* @see io.socket.IOTransport#disconnect()
39+
*/
40+
@Override
41+
public void disconnect() {
42+
try {
43+
this.close();
44+
} catch (Exception e) {
45+
connection.transportError(e);
46+
}
47+
}
48+
49+
/* (non-Javadoc)
50+
* @see io.socket.IOTransport#canSendBulk()
51+
*/
52+
@Override
53+
public boolean canSendBulk() {
54+
return false;
55+
}
56+
57+
/* (non-Javadoc)
58+
* @see io.socket.IOTransport#sendBulk(java.lang.String[])
59+
*/
60+
@Override
61+
public void sendBulk(String[] texts) throws IOException {
62+
throw new RuntimeException("Cannot send Bulk!");
63+
}
64+
65+
/* (non-Javadoc)
66+
* @see io.socket.IOTransport#invalidate()
67+
*/
68+
@Override
69+
public void invalidate() {
70+
connection = null;
71+
}
72+
73+
@Override
74+
public void onClose(int code, String reason, boolean remote) {
75+
if(connection != null)
76+
connection.transportDisconnected();
77+
}
78+
79+
@Override
80+
public void onMessage(String text) {
81+
if(connection != null)
82+
connection.transportMessage(text);
83+
}
84+
85+
@Override
86+
public void onOpen(ServerHandshake handshakedata) {
87+
if(connection != null)
88+
connection.transportConnected();
89+
}
90+
91+
@Override
92+
public String getName() {
93+
return TRANSPORT_NAME;
94+
}
95+
96+
@Override
97+
public void onError(Exception ex) {
98+
// TODO Auto-generated method stub
99+
100+
}
101+
}

src/io/socket/XhrTransport.java

+7
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();

tests/io/socket/AbstractTestSocketIO.java

+1-1
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ public abstract class AbstractTestSocketIO implements IOCallback {
3636
private static final String REQUEST_ACKNOWLEDGE = "requestAcknowledge";
3737

3838
/** The Constant to the node executable */
39-
private final static String NODE = "node";
39+
private final static String NODE = "/usr/local/bin/node";
4040

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

0 commit comments

Comments
 (0)