Skip to content

Commit 09e24ad

Browse files
committed
Support setting the default SSLSocketFactory for socket.io negotiation over HTTPS
?
1 parent 69d9f2e commit 09e24ad

File tree

2 files changed

+28
-1
lines changed

2 files changed

+28
-1
lines changed

src/io/socket/IOConnection.java

+17
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,9 @@
2626
import java.util.concurrent.ConcurrentLinkedQueue;
2727
import java.util.logging.Logger;
2828

29+
import javax.net.ssl.HttpsURLConnection;
30+
import javax.net.ssl.SSLSocketFactory;
31+
2932
import org.json.JSONArray;
3033
import org.json.JSONException;
3134
import org.json.JSONObject;
@@ -63,6 +66,9 @@ class IOConnection implements IOCallback {
6366
/** Socket.io path. */
6467
public static final String SOCKET_IO_1 = "/socket.io/1/";
6568

69+
/** The SSL socket factory for HTTPS connections */
70+
private static SSLSocketFactory sslSocketFactory = (SSLSocketFactory)SSLSocketFactory.getDefault();
71+
6672
/** All available connections. */
6773
private static HashMap<String, List<IOConnection>> connections = new HashMap<String, List<IOConnection>>();
6874

@@ -198,6 +204,14 @@ public void run() {
198204

199205
};
200206

207+
/**
208+
* Set the socket factory used for SSL connections.
209+
* @param socketFactory
210+
*/
211+
public static void setDefaultSSLSocketFactory(SSLSocketFactory socketFactory) {
212+
sslSocketFactory = socketFactory;
213+
}
214+
201215
/**
202216
* Creates a new connection or returns the corresponding one.
203217
*
@@ -273,6 +287,9 @@ private void handshake() {
273287
setState(STATE_HANDSHAKE);
274288
url = new URL(IOConnection.this.url.toString() + SOCKET_IO_1);
275289
connection = url.openConnection();
290+
if(connection instanceof HttpsURLConnection) {
291+
((HttpsURLConnection)connection).setSSLSocketFactory(sslSocketFactory);
292+
}
276293
connection.setConnectTimeout(connectTimeout);
277294
connection.setReadTimeout(connectTimeout);
278295

src/io/socket/SocketIO.java

+11-1
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,8 @@
1212
import java.net.URL;
1313
import java.util.Properties;
1414

15+
import javax.net.ssl.SSLSocketFactory;
16+
1517
import org.json.JSONObject;
1618

1719
/**
@@ -120,7 +122,15 @@ public SocketIO(final URL url, final IOCallback callback) {
120122
public SocketIO(final URL url) {
121123
setAndConnect(url, null);
122124
}
123-
125+
126+
/**
127+
* Set the socket factory used for SSL connections.
128+
* @param socketFactory
129+
*/
130+
public static void setDefaultSSLSocketFactory(SSLSocketFactory socketFactory) {
131+
IOConnection.setDefaultSSLSocketFactory(socketFactory);
132+
}
133+
124134
/**
125135
* connects to supplied host using callback. Do only use this method if you
126136
* instantiate {@link SocketIO} using {@link #SocketIO()}.

0 commit comments

Comments
 (0)