From a9e9e1207ce417388b1d8f03df5833cea24939b7 Mon Sep 17 00:00:00 2001
From: zj <1772600164@qq.com>
Date: Thu, 08 Aug 2024 01:10:12 +0800
Subject: [PATCH] 1
---
geteClient/src/main/java/org/example/geteclient/wsClinet/GateClient.java | 75 ++++++++++++++++++-------------------
1 files changed, 37 insertions(+), 38 deletions(-)
diff --git a/geteClient/src/main/java/org/example/geteclient/wsClinet/GateClient.java b/geteClient/src/main/java/org/example/geteclient/wsClinet/GateClient.java
index 3301db2..2c03b74 100644
--- a/geteClient/src/main/java/org/example/geteclient/wsClinet/GateClient.java
+++ b/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 {
--
Gitblit v1.9.3