From f6bd6d0ad7b21ed124afca7c30c8fcea93a56319 Mon Sep 17 00:00:00 2001
From: zj <1772600164@qq.com>
Date: Wed, 07 Aug 2024 18:35:34 +0800
Subject: [PATCH] 1

---
 kucoinClient/src/main/java/org/example/kucoinclient/wsClient/KucoinClient.java |   71 ++++++++++++++++++-----------------
 1 files changed, 37 insertions(+), 34 deletions(-)

diff --git a/kucoinClient/src/main/java/org/example/kucoinclient/wsClient/KucoinClient.java b/kucoinClient/src/main/java/org/example/kucoinclient/wsClient/KucoinClient.java
index e5042fa..90b3d5c 100644
--- a/kucoinClient/src/main/java/org/example/kucoinclient/wsClient/KucoinClient.java
+++ b/kucoinClient/src/main/java/org/example/kucoinclient/wsClient/KucoinClient.java
@@ -62,7 +62,7 @@
         this.executorService = Executors.newScheduledThreadPool(1); // 创建调度线程池
     }
 
-    public void start() throws Exception {
+    public void start() {
         try {
             connect(); // 连接到 WebSocket 服务器
             if (session == null) {
@@ -79,7 +79,6 @@
 
         } catch (Exception e) {
             log.error("kucoin ws 连接过程中发生异常:" + e.getMessage(), e); // 记录连接过程中发生的异常
-           throw e;
         } finally {
             executorService.shutdown(); // 关闭调度线程池
         }
@@ -130,7 +129,11 @@
         }
         if (map.get("data") != null) {
             Object object = map.get("data"); // 获取数据内容
-            processData(map.get("topic").toString(), object); // 处理数据
+            if(null == map.get("topic")){
+                System.out.println("-------------------出错了:"+map);
+            }else{
+                processData(map.get("topic").toString(), object); // 处理数据
+            }
         }
     }
 
@@ -199,44 +202,44 @@
     @OnClose
     public void onClose() throws Exception {
         log.info("kucoin ws 连接已关闭,尝试重新连接..."); // 输出连接关闭日志
-        throw new Exception();
+        handleConnectionClosedOrError();
     }
 
     @OnError
     public void onError(Throwable throwable) throws Exception {
         log.error("kucoin ws 发生错误: ", throwable); // 输出错误日志
-        throw new Exception();
+        handleConnectionClosedOrError();
     }
 
-//    private void handleConnectionClosedOrError() {
-//        synchronized (lock) { // 同步块,确保线程安全
-//            if (!reconnecting) {
-//                reconnecting = true; // 状态标记为重连中
-//                executorService.execute(this::attemptReconnect); // 执行重连操作
-//            }
-//        }
-//    }
-//
-//    private void attemptReconnect() {
-//        try {
-//            log.info("kucoin ws 开始重连"); // 输出重连开始日志
-//            connect(); // 尝试重新连接
-//            log.info("kucoin ws 重连成功"); // 输出重连成功日志
-//        } catch (Exception e) {
-//            log.error("kucoin ws 重连失败: ", e); // 输出重连失败日志
-//        } finally {
-//            synchronized (lock) { // 同步块
-//                reconnecting = false; // 状态标记为未重连
-//                scheduleReconnect(); // 重新调度重连任务
-//            }
-//        }
-//    }
-//
-//    private void scheduleReconnect() {
-//        if (!executorService.isShutdown()) { // 检查线程池是否已经关闭
-//            executorService.schedule(this::attemptReconnect, RECONNECT_DELAY, TimeUnit.SECONDS); // 调度重连
-//        }
-//    }
+    private void handleConnectionClosedOrError() {
+        synchronized (lock) { // 同步块,确保线程安全
+            if (!reconnecting) {
+                reconnecting = true; // 状态标记为重连中
+                executorService.execute(this::attemptReconnect); // 执行重连操作
+            }
+        }
+    }
+
+    private void attemptReconnect() {
+        try {
+            log.info("kucoin ws 开始重连"); // 输出重连开始日志
+            connect(); // 尝试重新连接
+            log.info("kucoin ws 重连成功"); // 输出重连成功日志
+        } catch (Exception e) {
+            log.error("kucoin ws 重连失败: ", e); // 输出重连失败日志
+        } finally {
+            synchronized (lock) { // 同步块
+                reconnecting = false; // 状态标记为未重连
+                scheduleReconnect(); // 重新调度重连任务
+            }
+        }
+    }
+
+    private void scheduleReconnect() {
+        if (!executorService.isShutdown()) { // 检查线程池是否已经关闭
+            executorService.schedule(this::attemptReconnect, RECONNECT_DELAY, TimeUnit.SECONDS); // 调度重连
+        }
+    }
 
     private void sendPing() {
         try {

--
Gitblit v1.9.3