|
49 | 49 | import org.eclipse.jetty.client.HttpClient; |
50 | 50 | import org.eclipse.jetty.http.HttpField; |
51 | 51 | import org.eclipse.jetty.http.HttpHeader; |
| 52 | +import org.eclipse.jetty.util.component.LifeCycle; |
52 | 53 | import org.eclipse.jetty.websocket.api.Session; |
53 | 54 | import org.eclipse.jetty.websocket.api.StatusCode; |
54 | 55 | import org.eclipse.jetty.websocket.api.WebSocketListener; |
@@ -92,6 +93,8 @@ public class ConnectionWrapper implements WebSocketListener, ConnectionInterface |
92 | 93 |
|
93 | 94 | private List<BlockingQueue<String>> streamQueues = new ArrayList<>(); |
94 | 95 |
|
| 96 | + private Timer timer; |
| 97 | + |
95 | 98 | public ConnectionWrapper(WebSocketClientConfiguration configuration, Gson gson) { |
96 | 99 | this(configuration, null, gson); |
97 | 100 | } |
@@ -160,8 +163,8 @@ public ConnectionWrapper( |
160 | 163 | } |
161 | 164 |
|
162 | 165 | Integer reconnectAfter = configuration.getReconnectIntervalTime(); |
163 | | - new Timer() |
164 | | - .scheduleAtFixedRate( |
| 166 | + this.timer = new Timer(); |
| 167 | + this.timer.scheduleAtFixedRate( |
165 | 168 | new TimerTask() { |
166 | 169 | @Override |
167 | 170 | public void run() { |
@@ -221,6 +224,7 @@ public void connect(Consumer<Session> customCallback) |
221 | 224 | CompletableFuture<Session> clientSessionPromise = |
222 | 225 | webSocketClient.connect(this, serverURI, clientUpgradeRequest); |
223 | 226 | Session session = clientSessionPromise.join(); |
| 227 | + |
224 | 228 | if (callback != null) { |
225 | 229 | callback.accept(session); |
226 | 230 | } |
@@ -501,6 +505,7 @@ protected void afterConnect(Session session) { |
501 | 505 | if (this.oldSession != null) { |
502 | 506 | this.oldSession.close(StatusCode.NORMAL, "close after reconnect", WriteCallback.NOOP); |
503 | 507 | } |
| 508 | + canReconnect = true; |
504 | 509 | setReady(true); |
505 | 510 | } |
506 | 511 |
|
@@ -548,7 +553,17 @@ public URI getUri(String uri) throws URISyntaxException { |
548 | 553 | public void disconnect() { |
549 | 554 | if (this.session != null) { |
550 | 555 | this.session.disconnect(); |
| 556 | + } |
| 557 | + setReady(false); |
| 558 | + canReconnect = false; |
| 559 | + } |
| 560 | + |
| 561 | + public void stop() throws Exception { |
| 562 | + if (this.webSocketClient != null) { |
551 | 563 | setReady(false); |
| 564 | + canReconnect = false; |
| 565 | + timer.cancel(); |
| 566 | + webSocketClient.stop(); |
552 | 567 | } |
553 | 568 | } |
554 | 569 | } |
0 commit comments