Skip to content

Commit c3b13e8

Browse files
authored
Merge pull request #2 from XDex/expose_configs
Expose connectionTimeout and perMessageDeflate configs
2 parents 3985c9c + 919e0cf commit c3b13e8

File tree

1 file changed

+30
-15
lines changed

1 file changed

+30
-15
lines changed

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

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

3840
private static final ObjectMapper mapper = new ObjectMapper();
3941

4042
public Socket(String URL) {
4143
this.URL = URL;
42-
factory = new WebSocketFactory().setConnectionTimeout(5000);
44+
factory = new WebSocketFactory();
4345
counter = new AtomicInteger(1);
4446
acks = new HashMap<>();
4547
channels = new ArrayList<>();
@@ -89,6 +91,21 @@ public void setCodec(SocketClusterCodec codec) {
8991
this.codec = codec;
9092
}
9193

94+
/**
95+
* Set Websocket connection timeout - set to 5000 by default
96+
* @param timeout - connection timeout in Milliseconds
97+
*/
98+
public void setConnectionTimeout(int timeout) {
99+
connectionTimeout = timeout;
100+
}
101+
102+
/**
103+
* Disable Websocket perMessageDeflate compression, which is enabled by default
104+
*/
105+
public void disablePerMessageDeflateCompression() {
106+
perMessageDeflate = false;
107+
}
108+
92109
/**
93110
* used to set up TLS/SSL connection to server for more details visit neovisionaries websocket client
94111
*/
@@ -411,20 +428,28 @@ public Map<String, String> getHeaders() {
411428
return headers;
412429
}
413430

414-
public void connect() {
415-
431+
private void setupConnection() {
416432
try {
433+
factory.setConnectionTimeout(connectionTimeout);
417434
ws = factory.createSocket(URL);
418435
} catch (IOException e) {
419436
e.printStackTrace();
420437
}
421-
ws.addExtension("permessage-deflate; client_max_window_bits");
438+
439+
if (perMessageDeflate) {
440+
ws.addExtension(WebSocketExtension.PERMESSAGE_DEFLATE);
441+
}
442+
ws.addExtension("client_max_window_bits");
443+
422444
for (Map.Entry<String, String> entry : headers.entrySet()) {
423445
ws.addHeader(entry.getKey(), entry.getValue());
424446
}
425447

426448
ws.addListener(adapter);
449+
}
427450

451+
public void connect() {
452+
setupConnection();
428453
try {
429454
ws.connect();
430455
} catch (OpeningHandshakeException e) {
@@ -468,17 +493,7 @@ public void connect() {
468493
}
469494

470495
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);
496+
setupConnection();
482497
ws.connectAsynchronously();
483498
}
484499

0 commit comments

Comments
 (0)