Skip to content

Commit 95e0a50

Browse files
committed
Use a SocketFactory to support reconnecting
Fixes TooTallNate#764
1 parent 64b7574 commit 95e0a50

File tree

6 files changed

+168
-5
lines changed

6 files changed

+168
-5
lines changed

.gitignore

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,5 @@ bin
1616
/doc
1717
*.xml
1818
.idea/.name
19-
*.jks
2019
lib/junit-4.7.jar
2120
*.jar

src/main/example/SSLClientExample.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -104,7 +104,7 @@ public static void main( String[] args ) throws Exception {
104104

105105
SSLSocketFactory factory = sslContext.getSocketFactory();// (SSLSocketFactory) SSLSocketFactory.getDefault();
106106

107-
chatclient.setSocket( factory.createSocket() );
107+
chatclient.setSocketFactory( factory );
108108

109109
chatclient.connectBlocking();
110110

src/main/java/org/java_websocket/client/WebSocketClient.java

Lines changed: 22 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,7 @@
4040
import java.util.concurrent.CountDownLatch;
4141
import java.util.concurrent.TimeUnit;
4242

43+
import javax.net.SocketFactory;
4344
import javax.net.ssl.SSLContext;
4445
import javax.net.ssl.SSLException;
4546
import javax.net.ssl.SSLSocketFactory;
@@ -79,6 +80,12 @@ public abstract class WebSocketClient extends AbstractWebSocket implements Runna
7980
*/
8081
private Socket socket = null;
8182

83+
/**
84+
* The SocketFactory for this WebSocketClient
85+
* @since 1.4.0
86+
*/
87+
private SocketFactory socketFactory = null;
88+
8289
/**
8390
* The used OutputStream
8491
*/
@@ -372,8 +379,9 @@ public void run() {
372379
InputStream istream;
373380
try {
374381
boolean isNewSocket = false;
375-
376-
if( socket == null ) {
382+
if (socketFactory != null) {
383+
socket = socketFactory.createSocket();
384+
} else if( socket == null ) {
377385
socket = new Socket( proxy );
378386
isNewSocket = true;
379387

@@ -691,14 +699,26 @@ public void setProxy( Proxy proxy ) {
691699
* This method must be called before <code>connect</code>.
692700
* If the given socket is not yet bound it will be bound to the uri specified in the constructor.
693701
* @param socket The socket which should be used for the connection
702+
* @deprecated use setSocketFactory
694703
*/
704+
@Deprecated
695705
public void setSocket( Socket socket ) {
696706
if( this.socket != null ) {
697707
throw new IllegalStateException( "socket has already been set" );
698708
}
699709
this.socket = socket;
700710
}
701711

712+
/**
713+
* Accepts a SocketFactory.<br>
714+
* This method must be called before <code>connect</code>.
715+
* The socket will be bound to the uri specified in the constructor.
716+
* @param socketFactory The socket factory which should be used for the connection.
717+
*/
718+
public void setSocketFactory(SocketFactory socketFactory) {
719+
this.socketFactory = socketFactory;
720+
}
721+
702722
@Override
703723
public void sendFragmentedFrame(Opcode op, ByteBuffer buffer, boolean fin ) {
704724
engine.sendFragmentedFrame( op, buffer, fin );

src/test/java/org/java_websocket/issues/AllIssueTests.java

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,9 @@
3737
org.java_websocket.issues.Issue661Test.class,
3838
org.java_websocket.issues.Issue666Test.class,
3939
org.java_websocket.issues.Issue677Test.class,
40-
org.java_websocket.issues.Issue732Test.class
40+
org.java_websocket.issues.Issue732Test.class,
41+
org.java_websocket.issues.Issue764Test.class,
42+
org.java_websocket.issues.Issue765Test.class
4143
})
4244
/**
4345
* Start all tests for issues
Lines changed: 142 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,142 @@
1+
/*
2+
* Copyright (c) 2010-2018 Nathan Rajlich
3+
*
4+
* Permission is hereby granted, free of charge, to any person
5+
* obtaining a copy of this software and associated documentation
6+
* files (the "Software"), to deal in the Software without
7+
* restriction, including without limitation the rights to use,
8+
* copy, modify, merge, publish, distribute, sublicense, and/or sell
9+
* copies of the Software, and to permit persons to whom the
10+
* Software is furnished to do so, subject to the following
11+
* conditions:
12+
*
13+
* The above copyright notice and this permission notice shall be
14+
* included in all copies or substantial portions of the Software.
15+
*
16+
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
17+
* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
18+
* OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
19+
* NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
20+
* HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
21+
* WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
22+
* FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
23+
* OTHER DEALINGS IN THE SOFTWARE.
24+
*
25+
*/
26+
27+
package org.java_websocket.issues;
28+
29+
import org.java_websocket.WebSocket;
30+
import org.java_websocket.client.WebSocketClient;
31+
import org.java_websocket.handshake.ClientHandshake;
32+
import org.java_websocket.handshake.ServerHandshake;
33+
import org.java_websocket.server.DefaultSSLWebSocketServerFactory;
34+
import org.java_websocket.server.WebSocketServer;
35+
import org.java_websocket.util.SocketUtil;
36+
import org.junit.Test;
37+
38+
import javax.net.ssl.KeyManagerFactory;
39+
import javax.net.ssl.SSLContext;
40+
import javax.net.ssl.TrustManagerFactory;
41+
import java.io.File;
42+
import java.io.FileInputStream;
43+
import java.io.IOException;
44+
import java.net.InetSocketAddress;
45+
import java.net.URI;
46+
import java.net.URISyntaxException;
47+
import java.security.*;
48+
import java.security.cert.CertificateException;
49+
import java.util.concurrent.CountDownLatch;
50+
51+
public class Issue764Test {
52+
private CountDownLatch countClientDownLatch = new CountDownLatch(2);
53+
private CountDownLatch countServerDownLatch = new CountDownLatch(1);
54+
55+
@Test(timeout = 2000)
56+
public void testIssue() throws IOException, URISyntaxException, KeyStoreException, NoSuchAlgorithmException, KeyManagementException, UnrecoverableKeyException, CertificateException, InterruptedException {
57+
int port = SocketUtil.getAvailablePort();
58+
final WebSocketClient webSocket = new WebSocketClient(new URI("wss://localhost:" + port)) {
59+
@Override
60+
public void onOpen(ServerHandshake handshakedata) {
61+
countClientDownLatch.countDown();
62+
countServerDownLatch.countDown();
63+
}
64+
65+
@Override
66+
public void onMessage(String message) {
67+
}
68+
69+
@Override
70+
public void onClose(int code, String reason, boolean remote) {
71+
}
72+
73+
@Override
74+
public void onError(Exception ex) {
75+
}
76+
};
77+
WebSocketServer server = new MyWebSocketServer(port, webSocket, countServerDownLatch);
78+
79+
// load up the key store
80+
String STORETYPE = "JKS";
81+
String KEYSTORE = String.format("src%1$stest%1$1sjava%1$1sorg%1$1sjava_websocket%1$1skeystore.jks", File.separator);
82+
String STOREPASSWORD = "storepassword";
83+
String KEYPASSWORD = "keypassword";
84+
85+
KeyStore ks = KeyStore.getInstance(STORETYPE);
86+
File kf = new File(KEYSTORE);
87+
ks.load(new FileInputStream(kf), STOREPASSWORD.toCharArray());
88+
89+
KeyManagerFactory kmf = KeyManagerFactory.getInstance("SunX509");
90+
kmf.init(ks, KEYPASSWORD.toCharArray());
91+
TrustManagerFactory tmf = TrustManagerFactory.getInstance("SunX509");
92+
tmf.init(ks);
93+
94+
SSLContext sslContext = null;
95+
sslContext = SSLContext.getInstance("TLS");
96+
sslContext.init(kmf.getKeyManagers(), tmf.getTrustManagers(), null);
97+
98+
server.setWebSocketFactory(new DefaultSSLWebSocketServerFactory(sslContext));
99+
webSocket.setSocketFactory(sslContext.getSocketFactory());
100+
server.start();
101+
countServerDownLatch.await();
102+
webSocket.connectBlocking();
103+
webSocket.reconnectBlocking();
104+
countClientDownLatch.await();
105+
}
106+
107+
108+
private static class MyWebSocketServer extends WebSocketServer {
109+
private final WebSocketClient webSocket;
110+
private final CountDownLatch countServerDownLatch;
111+
112+
113+
public MyWebSocketServer(int port, WebSocketClient webSocket, CountDownLatch countServerDownLatch) {
114+
super(new InetSocketAddress(port));
115+
this.webSocket = webSocket;
116+
this.countServerDownLatch = countServerDownLatch;
117+
}
118+
119+
@Override
120+
public void onOpen(WebSocket conn, ClientHandshake handshake) {
121+
}
122+
123+
@Override
124+
public void onClose(WebSocket conn, int code, String reason, boolean remote) {
125+
}
126+
127+
@Override
128+
public void onMessage(WebSocket conn, String message) {
129+
130+
}
131+
132+
@Override
133+
public void onError(WebSocket conn, Exception ex) {
134+
ex.printStackTrace();
135+
}
136+
137+
@Override
138+
public void onStart() {
139+
countServerDownLatch.countDown();
140+
}
141+
}
142+
}
2.2 KB
Binary file not shown.

0 commit comments

Comments
 (0)