Skip to content

Commit 41ac7e4

Browse files
committed
Added way to connect and close the websocket client synchronously.
1 parent 0b4964f commit 41ac7e4

File tree

2 files changed

+26
-11
lines changed

2 files changed

+26
-11
lines changed

src/main/example/SSLClientExample.java

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

8181
chatclient.setWebSocketFactory( new DefaultSSLWebSocketClientFactory( sslContext ) );
8282

83-
chatclient.connect();
83+
chatclient.connectBlocking();
8484

8585
BufferedReader reader = new BufferedReader( new InputStreamReader( System.in ) );
8686
while ( true ) {

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

Lines changed: 25 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
import java.util.List;
1818
import java.util.Map;
1919
import java.util.Set;
20+
import java.util.concurrent.CountDownLatch;
2021

2122
import org.java_websocket.SocketChannelIOHelper;
2223
import org.java_websocket.WebSocket;
@@ -71,6 +72,10 @@ public abstract class WebSocketClient extends WebSocketAdapter implements Runnab
7172

7273
private Map<String,String> headers;
7374

75+
private CountDownLatch connectLatch = new CountDownLatch( 1 );
76+
77+
private CountDownLatch closeLatch = new CountDownLatch( 1 );
78+
7479
WebSocketClientFactory wf = new WebSocketClientFactory() {
7580
@Override
7681
public WebSocket createWebSocket( WebSocketAdapter a, Draft d, Socket s ) {
@@ -134,23 +139,30 @@ public Draft getDraft() {
134139
*/
135140
public void connect() {
136141
if( thread != null )
137-
throw new IllegalStateException( "already/still connected" );
142+
throw new IllegalStateException( "WebSocketClient objects are not reuseable" );
138143
thread = new Thread( this );
139144
thread.start();
140145
}
141146

147+
/**
148+
* Same as connect but blocks until the websocket connected or failed to do so.<br>
149+
* Returns whether it succeeded or not.
150+
**/
151+
public boolean connectBlocking() throws InterruptedException {
152+
connect();
153+
connectLatch.await();
154+
return conn.isOpen();
155+
}
156+
142157
public void close() {
143-
if( thread != null ) {
158+
if( thread != null && conn != null ) {
144159
conn.close( CloseFrame.NORMAL );
145-
/*closelock.lock();
146-
try {
147-
if( selector != null )
148-
selector.wakeup();
149-
} finally {
150-
closelock.unlock();
151-
}*/
152160
}
161+
}
153162

163+
public void closeBlocking() throws InterruptedException {
164+
close();
165+
closeLatch.await();
154166
}
155167

156168
/**
@@ -361,6 +373,7 @@ public final void onWebsocketMessage( WebSocket conn, ByteBuffer blob ) {
361373
*/
362374
@Override
363375
public final void onWebsocketOpen( WebSocket conn, Handshakedata handshake ) {
376+
connectLatch.countDown();
364377
onOpen( (ServerHandshake) handshake );
365378
}
366379

@@ -371,6 +384,8 @@ public final void onWebsocketOpen( WebSocket conn, Handshakedata handshake ) {
371384
*/
372385
@Override
373386
public final void onWebsocketClose( WebSocket conn, int code, String reason, boolean remote ) {
387+
connectLatch.countDown();
388+
closeLatch.countDown();
374389
onClose( code, reason, remote );
375390
}
376391

@@ -390,7 +405,7 @@ public final void onWriteDemand( WebSocket conn ) {
390405
key.interestOps( SelectionKey.OP_READ | SelectionKey.OP_WRITE );
391406
selector.wakeup();
392407
} catch ( CancelledKeyException e ) {
393-
// since such an exception/event will also occur on the selector there is no need to do anything here
408+
// since such an exception/event will also occur on the selector there is no need to do anything herec
394409
}
395410
}
396411

0 commit comments

Comments
 (0)