From 8ed60795a7c278f9699616f72eb05ce49800ba6f Mon Sep 17 00:00:00 2001
From: zj <1772600164@qq.com>
Date: Wed, 07 Aug 2024 01:20:50 +0800
Subject: [PATCH] 1
---
bitgetsClient/src/main/java/org/example/bitgetsclient/wsClient/BitgetClient.java | 91 +++++++++++++++++++++++++--------------------
1 files changed, 50 insertions(+), 41 deletions(-)
diff --git a/bitgetsClient/src/main/java/org/example/bitgetsclient/wsClient/BitgetClient.java b/bitgetsClient/src/main/java/org/example/bitgetsclient/wsClient/BitgetClient.java
index aea4698..c3cb229 100644
--- a/bitgetsClient/src/main/java/org/example/bitgetsclient/wsClient/BitgetClient.java
+++ b/bitgetsClient/src/main/java/org/example/bitgetsclient/wsClient/BitgetClient.java
@@ -8,9 +8,16 @@
import com.google.gson.Gson;
import com.google.gson.JsonSyntaxException;
import lombok.extern.slf4j.Slf4j;
+import org.example.bitgetsclient.BitgetsClientApplication;
+import org.example.bitgetsclient.comm.ApplicationContextProvider;
import org.example.bitgetsclient.util.RedisUtil;
import org.json.JSONException;
+import org.springframework.beans.factory.annotation.Autowired;
+import org.springframework.boot.SpringApplication;
+import org.springframework.context.ApplicationContext;
+import org.springframework.context.ConfigurableApplicationContext;
import org.springframework.context.annotation.Bean;
+import org.springframework.stereotype.Component;
import org.springframework.util.CollectionUtils;
import javax.websocket.*;
@@ -26,6 +33,7 @@
import java.util.concurrent.Executors;
import java.util.concurrent.ScheduledExecutorService;
import java.util.concurrent.TimeUnit;
+import java.util.concurrent.atomic.AtomicInteger;
@ClientEndpoint
@Slf4j
@@ -55,7 +63,7 @@
- public void start() {
+ public void start() throws Exception {
try {
connect(); // 尝试连接
if (session == null) {
@@ -67,7 +75,6 @@
executorService.scheduleAtFixedRate(this::sendPing, PING_INTERVAL, PING_INTERVAL, TimeUnit.MILLISECONDS);
// 发送订阅消息
session.getBasicRemote().sendText(subscriptions); // 发送订阅信息
-
synchronized (this) {
this.wait(); // 等待 WebSocket 消息到来
}
@@ -77,6 +84,7 @@
log.error("线程被中断: " + e.getMessage(), e); // 记录线程中断的错误
} catch (Exception e) {
log.error("bitget ws 连接过程中发生异常: " + e.getMessage(), e); // 记录连接过程中发生的异常
+ throw e;
} finally {
executorService.shutdownNow(); // 尝试立即关闭调度服务
}
@@ -109,8 +117,8 @@
if (dataNode != null) {
Map<String, Object> hashMap = new HashMap<>(); // 变量命名更具描述性
- String bids = dataNode.get("bids").toString();
- String asks = dataNode.get("asks").toString();
+ String asks = dataNode.get("bids").toString();
+ String bids = dataNode.get("asks").toString();
Type listType = new TypeToken<List<List<String>>>(){}.getType();
List<List<String>> bidsList = gson.fromJson(bids, listType);
@@ -167,51 +175,52 @@
}
@OnClose
- public void onClose() {
+ public void onClose() throws Exception {
log.info("bitget ws 连接已关闭,尝试重新连接..."); // 记录连接关闭的信息
- handleConnectionClosedOrError(); // 处理连接关闭事件
+ throw new Exception();
}
@OnError
- public void onError(Throwable throwable) {
+ public void onError(Throwable throwable) throws Exception {
log.error("bitget ws 发生错误: " + throwable.getMessage(), throwable); // 记录错误信息
- handleConnectionClosedOrError(); // 处理错误事件
+ throw new Exception();
}
- private void handleConnectionClosedOrError() {
- synchronized (lock) {
- if (!reconnecting) {
- reconnecting = true; // 开始重连
- executorService.execute(this::attemptReconnect); // 执行重连操作
- }
- }
- }
+// private void handleConnectionClosedOrError() {
+// executorService.execute(() -> {
+// try {
+// attemptReconnect();
+// } catch (Exception e) {
+// throw new RuntimeException(e);
+// }
+// });
+// }
- private void attemptReconnect() {
- if (reconnectAttempts < MAX_RECONNECT_ATTEMPTS) { // 检查重连次数是否超过限制
- try {
- log.info("bitget ws 开始重连"); // 记录开始重连的信息
- connect(); // 尝试重连
- log.info("bitget ws 重连成功"); // 成功重连的日志
- reconnectAttempts = 0; // 重连成功,重置重连次数
- } catch (Exception e) {
- reconnectAttempts++; // 增加重连尝试次数
- log.warn("bitget ws 重连失败,尝试次数: " + reconnectAttempts, e); // 记录重连失败的警告信息
- // 采用指数退避策略,增加重连间隔
- long waitTime = Math.min(60, (long) Math.pow(2, reconnectAttempts)) * 1000; // 最大等待时间为 60 秒
- scheduleReconnect(waitTime); // 调度下次重连
- }
- } else {
- log.error("超过最大重连次数,停止重连"); // 超过最大重连次数
- reconnecting = false; // 重连状态重置
- }
- }
-
- private void scheduleReconnect(long waitTime) { // 接受等待时间参数
- if (!executorService.isShutdown()) {
- executorService.schedule(this::attemptReconnect, waitTime, TimeUnit.MILLISECONDS); // 调度重连
- }
- }
+// private void attemptReconnect() throws Exception {
+// if (reconnectAttempts < MAX_RECONNECT_ATTEMPTS) { // 检查重连次数是否超过限制
+// try {
+// log.info("bitget ws 开始重连"); // 记录开始重连的信息
+// connect(); // 尝试重连
+// log.info("bitget ws 重连成功"); // 成功重连的日志
+// reconnectAttempts = 0; // 重连成功,重置重连次数
+// } catch (Exception e) {
+// reconnectAttempts++; // 增加重连尝试次数
+// log.warn("bitget ws 重连失败,尝试次数: " + reconnectAttempts, e); // 记录重连失败的警告信息
+// // 采用指数退避策略,增加重连间隔
+// long waitTime = Math.min(60, (long) Math.pow(2, reconnectAttempts)) * 1000; // 最大等待时间为 60 秒
+// scheduleReconnect(waitTime); // 调度下次重连
+// }
+// } else {
+// log.error("超过最大重连次数,停止重连"); // 超过最大重连次数
+// throw new Exception();
+// }
+// }
+//
+// private void scheduleReconnect(long waitTime) { // 接受等待时间参数
+// if (!executorService.isShutdown()) {
+// executorService.schedule(this::attemptReconnect, waitTime, TimeUnit.MILLISECONDS); // 调度重连
+// }
+// }
private void sendPing() {
try {
--
Gitblit v1.9.3