Skip to content

Commit

Permalink
Merge pull request TooTallNate#724 from cymp/master
Browse files Browse the repository at this point in the history
added a timeout option for connectBlocking
  • Loading branch information
PhilipRoman authored Jun 27, 2018
2 parents a3a83fa + 7961e87 commit 41b2872
Showing 1 changed file with 15 additions and 0 deletions.
15 changes: 15 additions & 0 deletions src/main/java/org/java_websocket/client/WebSocketClient.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -281,6 +282,20 @@ public boolean connectBlocking() throws InterruptedException {
return engine.isOpen();
}

/**
* Same as <code>connect</code> but blocks with a timeout until the websocket connected or failed to do so.<br>
* @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<br>
* In oder to make sure the connection is closed use <code>closeBlocking</code>
Expand Down

0 comments on commit 41b2872

Please sign in to comment.