Skip to content

Commit e21228e

Browse files
committed
Expose connectionTimeout setting
1 parent 3985c9c commit e21228e

File tree

1 file changed

+12
-14
lines changed

1 file changed

+12
-14
lines changed

src/main/java/io/github/sac/Socket.java

Lines changed: 12 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -34,12 +34,13 @@ public class Socket extends Emitter {
3434
private WebSocketAdapter adapter;
3535
private Map<String, String> headers;
3636
private SocketClusterCodec codec;
37+
private int connectionTimeout = 5000;
3738

3839
private static final ObjectMapper mapper = new ObjectMapper();
3940

4041
public Socket(String URL) {
4142
this.URL = URL;
42-
factory = new WebSocketFactory().setConnectionTimeout(5000);
43+
factory = new WebSocketFactory();
4344
counter = new AtomicInteger(1);
4445
acks = new HashMap<>();
4546
channels = new ArrayList<>();
@@ -89,6 +90,10 @@ public void setCodec(SocketClusterCodec codec) {
8990
this.codec = codec;
9091
}
9192

93+
public void setConnectionTimeout(int timeout) {
94+
connectionTimeout = timeout;
95+
}
96+
9297
/**
9398
* used to set up TLS/SSL connection to server for more details visit neovisionaries websocket client
9499
*/
@@ -411,9 +416,9 @@ public Map<String, String> getHeaders() {
411416
return headers;
412417
}
413418

414-
public void connect() {
415-
419+
private void setupConnection() {
416420
try {
421+
factory.setConnectionTimeout(connectionTimeout);
417422
ws = factory.createSocket(URL);
418423
} catch (IOException e) {
419424
e.printStackTrace();
@@ -424,7 +429,10 @@ public void connect() {
424429
}
425430

426431
ws.addListener(adapter);
432+
}
427433

434+
public void connect() {
435+
setupConnection();
428436
try {
429437
ws.connect();
430438
} catch (OpeningHandshakeException e) {
@@ -468,17 +476,7 @@ public void connect() {
468476
}
469477

470478
public void connectAsync() {
471-
try {
472-
ws = factory.createSocket(URL);
473-
} catch (IOException e) {
474-
e.printStackTrace();
475-
}
476-
ws.addExtension("permessage-deflate; client_max_window_bits");
477-
for (Map.Entry<String, String> entry : headers.entrySet()) {
478-
ws.addHeader(entry.getKey(), entry.getValue());
479-
}
480-
481-
ws.addListener(adapter);
479+
setupConnection();
482480
ws.connectAsynchronously();
483481
}
484482

0 commit comments

Comments
 (0)