From ff6665e1017c68e5e56daf1e6d9e0e88e4a5445c Mon Sep 17 00:00:00 2001 From: Cyril Date: Tue, 26 Jun 2018 08:56:06 +0200 Subject: [PATCH 1/2] added a timeout option for connectBlocking --- .../java_websocket/client/WebSocketClient.java | 15 +++++++++++++++ 1 file changed, 15 insertions(+) diff --git a/src/main/java/org/java_websocket/client/WebSocketClient.java b/src/main/java/org/java_websocket/client/WebSocketClient.java index 00e40fda0..234b39c14 100644 --- a/src/main/java/org/java_websocket/client/WebSocketClient.java +++ b/src/main/java/org/java_websocket/client/WebSocketClient.java @@ -38,6 +38,7 @@ import java.util.Collections; import java.util.Map; import java.util.concurrent.CountDownLatch; +import java.util.concurrent.TimeUnit; import javax.net.ssl.SSLContext; import javax.net.ssl.SSLException; @@ -281,6 +282,20 @@ public boolean connectBlocking() throws InterruptedException { return engine.isOpen(); } + /** + * Same as connect but blocks with a timeout until the websocket connected or failed to do so.
+ * @param timeout + * The connect timeout + * @param timeUnit + * The timeout time unit + * @return Returns whether it succeeded or not. + * @throws InterruptedException Thrown when the threads get interrupted + */ + public boolean connectBlocking(long timeout, TimeUnit timeUnit) throws InterruptedException { + connect(); + return connectLatch.await(timeout, timeUnit) && engine.isOpen(); + } + /** * Initiates the websocket close handshake. This method does not block
* In oder to make sure the connection is closed use closeBlocking From 7961e870434edd9059dfb7321ea70ea695b00ac1 Mon Sep 17 00:00:00 2001 From: cymp Date: Tue, 26 Jun 2018 09:22:27 +0200 Subject: [PATCH 2/2] formatted --- .../client/WebSocketClient.java | 26 +++++++++---------- 1 file changed, 13 insertions(+), 13 deletions(-) diff --git a/src/main/java/org/java_websocket/client/WebSocketClient.java b/src/main/java/org/java_websocket/client/WebSocketClient.java index 234b39c14..52dbe61e3 100644 --- a/src/main/java/org/java_websocket/client/WebSocketClient.java +++ b/src/main/java/org/java_websocket/client/WebSocketClient.java @@ -282,19 +282,19 @@ public boolean connectBlocking() throws InterruptedException { return engine.isOpen(); } - /** - * Same as connect but blocks with a timeout until the websocket connected or failed to do so.
- * @param timeout - * The connect timeout - * @param timeUnit - * The timeout time unit - * @return Returns whether it succeeded or not. - * @throws InterruptedException Thrown when the threads get interrupted - */ - public boolean connectBlocking(long timeout, TimeUnit timeUnit) throws InterruptedException { - connect(); - return connectLatch.await(timeout, timeUnit) && engine.isOpen(); - } + /** + * Same as connect but blocks with a timeout until the websocket connected or failed to do so.
+ * @param timeout + * The connect timeout + * @param timeUnit + * The timeout time unit + * @return Returns whether it succeeded or not. + * @throws InterruptedException Thrown when the threads get interrupted + */ + public boolean connectBlocking(long timeout, TimeUnit timeUnit) throws InterruptedException { + connect(); + return connectLatch.await(timeout, timeUnit) && engine.isOpen(); + } /** * Initiates the websocket close handshake. This method does not block