Skip to content

Commit bfeb22b

Browse files
authored
Merge pull request #197 from binance/websocket_stop
Add method to common to stop websocket client
2 parents c4572b8 + af92242 commit bfeb22b

File tree

19 files changed

+57
-22
lines changed

19 files changed

+57
-22
lines changed

clients/common/pom.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,6 @@
1010

1111
<artifactId>binance-common</artifactId>
1212
<name>common</name>
13-
<version>2.1.1</version>
13+
<version>2.2.1</version>
1414
<packaging>jar</packaging>
1515
</project>

clients/common/src/main/java/com/binance/connector/client/common/websocket/adapter/ConnectionInterface.java

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,4 +24,6 @@ public interface ConnectionInterface {
2424
void setLogonMethods(List<String> logonMethods);
2525

2626
void setLogoutMethods(List<String> logoutMethods);
27+
28+
void stop() throws Exception;
2729
}

clients/common/src/main/java/com/binance/connector/client/common/websocket/adapter/ConnectionWrapper.java

Lines changed: 17 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,7 @@
4949
import org.eclipse.jetty.client.HttpClient;
5050
import org.eclipse.jetty.http.HttpField;
5151
import org.eclipse.jetty.http.HttpHeader;
52+
import org.eclipse.jetty.util.component.LifeCycle;
5253
import org.eclipse.jetty.websocket.api.Session;
5354
import org.eclipse.jetty.websocket.api.StatusCode;
5455
import org.eclipse.jetty.websocket.api.WebSocketListener;
@@ -92,6 +93,8 @@ public class ConnectionWrapper implements WebSocketListener, ConnectionInterface
9293

9394
private List<BlockingQueue<String>> streamQueues = new ArrayList<>();
9495

96+
private Timer timer;
97+
9598
public ConnectionWrapper(WebSocketClientConfiguration configuration, Gson gson) {
9699
this(configuration, null, gson);
97100
}
@@ -160,8 +163,8 @@ public ConnectionWrapper(
160163
}
161164

162165
Integer reconnectAfter = configuration.getReconnectIntervalTime();
163-
new Timer()
164-
.scheduleAtFixedRate(
166+
this.timer = new Timer();
167+
this.timer.scheduleAtFixedRate(
165168
new TimerTask() {
166169
@Override
167170
public void run() {
@@ -221,6 +224,7 @@ public void connect(Consumer<Session> customCallback)
221224
CompletableFuture<Session> clientSessionPromise =
222225
webSocketClient.connect(this, serverURI, clientUpgradeRequest);
223226
Session session = clientSessionPromise.join();
227+
224228
if (callback != null) {
225229
callback.accept(session);
226230
}
@@ -501,6 +505,7 @@ protected void afterConnect(Session session) {
501505
if (this.oldSession != null) {
502506
this.oldSession.close(StatusCode.NORMAL, "close after reconnect", WriteCallback.NOOP);
503507
}
508+
canReconnect = true;
504509
setReady(true);
505510
}
506511

@@ -548,7 +553,17 @@ public URI getUri(String uri) throws URISyntaxException {
548553
public void disconnect() {
549554
if (this.session != null) {
550555
this.session.disconnect();
556+
}
557+
setReady(false);
558+
canReconnect = false;
559+
}
560+
561+
public void stop() throws Exception {
562+
if (this.webSocketClient != null) {
551563
setReady(false);
564+
canReconnect = false;
565+
timer.cancel();
566+
webSocketClient.stop();
552567
}
553568
}
554569
}

clients/common/src/main/java/com/binance/connector/client/common/websocket/adapter/PoolConnectionWrapper.java

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -147,4 +147,12 @@ public void setLogoutMethods(List<String> logoutMethods) {
147147
public boolean isConnected() {
148148
return isConnected;
149149
}
150+
151+
@Override
152+
public void stop() throws Exception {
153+
for (ConnectionWrapper connectionWrapper : connectionList) {
154+
connectionWrapper.stop();
155+
}
156+
isConnected = false;
157+
}
150158
}

clients/common/src/main/java/com/binance/connector/client/common/websocket/adapter/stream/StreamConnectionInterface.java

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,4 +21,6 @@ Map<String, StreamBlockingQueue<String>> subscribe(
2121
boolean isConnected();
2222

2323
void setUserAgent(String userAgent);
24+
25+
void stop() throws Exception;
2426
}

clients/common/src/main/java/com/binance/connector/client/common/websocket/adapter/stream/StreamConnectionPoolWrapper.java

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -154,4 +154,12 @@ public void setUserAgent(String userAgent) {
154154
public boolean isConnected() {
155155
return isConnected;
156156
}
157+
158+
@Override
159+
public void stop() throws Exception {
160+
for (ConnectionWrapper connectionWrapper : connectionList) {
161+
connectionWrapper.stop();
162+
}
163+
isConnected = false;
164+
}
157165
}

clients/common/src/main/java/com/binance/connector/client/common/websocket/adapter/stream/StreamConnectionWrapper.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -129,7 +129,7 @@ public void onWebSocketText(String message) {
129129

130130
protected void beforeConnect() {
131131
// no session, so this is not a reconnect
132-
if (this.session == null) {
132+
if (this.session == null || !this.session.isOpen()) {
133133
return;
134134
}
135135
RequestWrapperDTO<Object, HashSet<String>> listSubscriptions =

clients/derivatives-trading-coin-futures/docs/rest-api/migration-guide.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ With the transition to a modularized structure, the Binance Connector has been s
2222
<dependency>
2323
<groupId>io.github.binance</groupId>
2424
<artifactId>binance-derivatives-trading-coin-futures</artifactId>
25-
<version>4.0.0</version>
25+
<version>4.0.1</version>
2626
</dependency>
2727
```
2828

clients/derivatives-trading-coin-futures/pom.xml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
<modelVersion>4.0.0</modelVersion>
66
<artifactId>binance-derivatives-trading-coin-futures</artifactId>
77
<name>derivatives-trading-coin-futures</name>
8-
<version>4.0.0</version>
8+
<version>4.0.1</version>
99
<packaging>jar</packaging>
1010

1111
<parent>
@@ -31,7 +31,7 @@
3131
<dependency>
3232
<groupId>io.github.binance</groupId>
3333
<artifactId>binance-common</artifactId>
34-
<version>2.1.1</version>
34+
<version>2.2.1</version>
3535
</dependency>
3636
</dependencies>
3737
</project>

clients/derivatives-trading-options/docs/rest-api/migration-guide.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ With the transition to a modularized structure, the Binance Connector has been s
2222
<dependency>
2323
<groupId>io.github.binance</groupId>
2424
<artifactId>binance-derivatives-trading-options</artifactId>
25-
<version>4.0.0</version>
25+
<version>4.0.1</version>
2626
</dependency>
2727
```
2828

0 commit comments

Comments
 (0)