Skip to content

Commit ff6665e

Browse files
committed
added a timeout option for connectBlocking
1 parent a3a83fa commit ff6665e

File tree

1 file changed

+15
-0
lines changed

1 file changed

+15
-0
lines changed

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

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,7 @@
3838
import java.util.Collections;
3939
import java.util.Map;
4040
import java.util.concurrent.CountDownLatch;
41+
import java.util.concurrent.TimeUnit;
4142

4243
import javax.net.ssl.SSLContext;
4344
import javax.net.ssl.SSLException;
@@ -281,6 +282,20 @@ public boolean connectBlocking() throws InterruptedException {
281282
return engine.isOpen();
282283
}
283284

285+
/**
286+
* Same as <code>connect</code> but blocks with a timeout until the websocket connected or failed to do so.<br>
287+
* @param timeout
288+
* The connect timeout
289+
* @param timeUnit
290+
* The timeout time unit
291+
* @return Returns whether it succeeded or not.
292+
* @throws InterruptedException Thrown when the threads get interrupted
293+
*/
294+
public boolean connectBlocking(long timeout, TimeUnit timeUnit) throws InterruptedException {
295+
connect();
296+
return connectLatch.await(timeout, timeUnit) && engine.isOpen();
297+
}
298+
284299
/**
285300
* Initiates the websocket close handshake. This method does not block<br>
286301
* In oder to make sure the connection is closed use <code>closeBlocking</code>

0 commit comments

Comments
 (0)