Use a SocketFactory to support reconnecting · java-codehunger/Java-WebSocket@95e0a50 · GitHub
Skip to content

Commit 95e0a50

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

6 files changed

Lines changed: 168 additions & 5 deletions

File tree

.gitignore

Lines changed: 0 additions & 1 deletion

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
2.2 KB
Binary file not shown.

0 commit comments

Comments
 (0)