1
zj
2024-09-27 d51872d16007338a42099521bcb2d1460264b337
geteClient/src/main/java/org/example/geteclient/wsClinet/GateClient.java
@@ -53,7 +53,7 @@
        this.executorService = Executors.newScheduledThreadPool(1); // 创建一个定时任务调度线程池
    }
    public void start() throws Exception {
    public void start() {
        try {
            connect(); // 尝试连接 WebSocket
            if (session == null) { // 如果连接失败,session 仍然为 null
@@ -76,7 +76,6 @@
        } catch (Exception e) {
            log.error("gate ws 连接过程中发生异常: " + e.getMessage(), e); // 记录连接过程中发生的异常
            throw e;
        } finally {
            executorService.shutdown(); // 确保定时任务调度服务被关闭
        }
@@ -153,49 +152,49 @@
    @OnClose
    public void onClose() throws Exception {
        log.info("gate ws 连接已关闭,尝试重新连接..."); // 连接关闭日志
        throw new Exception();
        handleConnectionClosedOrError();
    }
    @OnError
    public void onError(Throwable throwable) throws Exception {
        log.error("gate ws 发生错误: " + throwable.getMessage(), throwable); // 记录错误日志
        throw new Exception();
        handleConnectionClosedOrError();
    }
//    private void handleConnectionClosedOrError() {
//        synchronized (lock) { // 进入同步块以防止并发重连
//            if (!reconnecting) { // 检查当前是否已经在重连
//                reconnecting = true; // 设置 reconnecting 为 true 表示开始重连
//                executorService.execute(this::attemptReconnect); // 使用 execute 方法立即执行重连
//            }
//        }
//    }
//
//    private void attemptReconnect() {
//        boolean doReconnect = true; // 是否进行重连的标志
//        try {
//            log.info("gate ws 开始重连"); // 开始重连日志
//            connect(); // 尝试重新连接
//            log.info("gate ws 重连成功"); // 重连成功日志
//        } catch (Exception e) {
//            log.error("gate ws 重连失败", e); // 重连失败记录日志
//            doReconnect = false; // 标记不再继续重连
//        } finally {
//            synchronized (lock) { // 进入同步块
//                if (doReconnect) {
//                    scheduleReconnect(); // 如果需要继续重连,调度重连任务
//                } else {
//                    reconnecting = false; // 重连结束,标记重连状态为 false
//                }
//            }
//        }
//    }
//
//    private void scheduleReconnect() {
//        if (!executorService.isShutdown()) { // 确保调度服务未关闭
//            executorService.schedule(this::attemptReconnect, 5, TimeUnit.SECONDS); // 重连调度,5秒后尝试重新连接
//        }
//    }
    private void handleConnectionClosedOrError() {
        synchronized (lock) { // 进入同步块以防止并发重连
            if (!reconnecting) { // 检查当前是否已经在重连
                reconnecting = true; // 设置 reconnecting 为 true 表示开始重连
                executorService.execute(this::attemptReconnect); // 使用 execute 方法立即执行重连
            }
        }
    }
    private void attemptReconnect() {
        boolean doReconnect = true; // 是否进行重连的标志
        try {
            log.info("gate ws 开始重连"); // 开始重连日志
            connect(); // 尝试重新连接
            log.info("gate ws 重连成功"); // 重连成功日志
        } catch (Exception e) {
            log.error("gate ws 重连失败", e); // 重连失败记录日志
            doReconnect = false; // 标记不再继续重连
        } finally {
            synchronized (lock) { // 进入同步块
                if (doReconnect) {
                    scheduleReconnect(); // 如果需要继续重连,调度重连任务
                } else {
                    reconnecting = false; // 重连结束,标记重连状态为 false
                }
            }
        }
    }
    private void scheduleReconnect() {
        if (!executorService.isShutdown()) { // 确保调度服务未关闭
            executorService.schedule(this::attemptReconnect, 5, TimeUnit.SECONDS); // 重连调度,5秒后尝试重新连接
        }
    }
    private void sendPing() {
        try {