1
zj
2024-08-07 f6bd6d0ad7b21ed124afca7c30c8fcea93a56319
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 {