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
---
mexcClient/src/main/java/org/example/mexcclient/wsClient/MexcClient.java | 95 ++++++++++++++++++++++++++---------------------
1 files changed, 52 insertions(+), 43 deletions(-)
diff --git a/mexcClient/src/main/java/org/example/mexcclient/wsClient/MexcClient.java b/mexcClient/src/main/java/org/example/mexcclient/wsClient/MexcClient.java
index 09cf860..8f2a2c1 100644
--- a/mexcClient/src/main/java/org/example/mexcclient/wsClient/MexcClient.java
+++ b/mexcClient/src/main/java/org/example/mexcclient/wsClient/MexcClient.java
@@ -8,8 +8,15 @@
import com.google.gson.Gson;
import com.google.gson.JsonSyntaxException;
import lombok.extern.slf4j.Slf4j;
+import org.example.mexcclient.MexcClientApplication;
+import org.example.mexcclient.comm.ApplicationContextProvider;
import org.example.mexcclient.pojo.Currency;
import org.example.mexcclient.util.RedisUtil;
+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.stereotype.Component;
import javax.websocket.*;
import java.io.IOException;
@@ -25,6 +32,7 @@
@ClientEndpoint
@Slf4j
+@Component
public class MexcClient {
private static final String WS_ENDPOINT = "wss://wbs.mexc.com/ws";
private static final long PING_INTERVAL = 20000;
@@ -41,7 +49,7 @@
this.executorService = Executors.newScheduledThreadPool(1);
}
- public void start() {
+ public void start() throws Exception {
try {
connect();
if (session == null) {
@@ -62,7 +70,8 @@
}
} catch (Exception e) {
- log.error("mexc ws 连接过程中发生异常: " + e.getMessage(), e);
+ log.error("mexc ws 连接过程中发生异常:" + e.getMessage(), e); // 记录连接过程中发生的异常
+ throw e;
} finally {
executorService.shutdown();
}
@@ -94,8 +103,8 @@
Object object = map.get("d");
Map<String, Object> resultMap = gson.fromJson(object.toString(), new TypeToken<Map<String, Object>>() {}.getType());
HashMap<String,Object> hashMap = new HashMap<>();
- Object asksObj = resultMap.get("asks");
- Object bidsObj = resultMap.get("bids");
+ Object bidsObj = resultMap.get("asks");
+ Object asksObj = resultMap.get("bids");
Type listType = new TypeToken<List<Map<String,Object>>>(){}.getType();
List<Map<String,Object>> asksList = gson.fromJson(asksObj.toString(), listType);
@@ -135,52 +144,52 @@
}
@OnClose
- public void onClose() {
+ public void onClose() throws Exception {
log.info("mexc ws 连接已关闭,尝试重新连接...");
- handleConnectionClosedOrError();
+ throw new Exception();
}
@OnError
- public void onError(Throwable throwable) {
+ public void onError(Throwable throwable) throws Exception {
log.error("mexc ws 发生错误: " + throwable.getMessage(), throwable);
- handleConnectionClosedOrError();
+ throw new Exception();
}
- 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("mexc ws 开始重连");
- connect(); // 假设 connect() 方法用于实际的连接逻辑
- log.info("mexc ws 重连成功");
- } catch (Exception e) {
- log.error("mexc ws 重连失败", e);
- // 连接失败时,可以根据具体情况决定是否继续重连
- // 在这里假设总是继续尝试重连
- } finally {
- synchronized (lock) {
- if (doReconnect) {
- scheduleReconnect(); // 如果需要继续重连,则重新调度重连任务
- } else {
- reconnecting = false; // 重连结束后设置 reconnecting 为 false
- }
- }
- }
- }
-
- private void scheduleReconnect() {
- if (!executorService.isShutdown()) {
- executorService.schedule(this::attemptReconnect, 5, TimeUnit.SECONDS);
- }
- }
+// 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("mexc ws 开始重连");
+// connect(); // 假设 connect() 方法用于实际的连接逻辑
+// log.info("mexc ws 重连成功");
+// } catch (Exception e) {
+// log.error("mexc ws 重连失败", e);
+// // 连接失败时,可以根据具体情况决定是否继续重连
+// // 在这里假设总是继续尝试重连
+// } finally {
+// synchronized (lock) {
+// if (doReconnect) {
+// scheduleReconnect(); // 如果需要继续重连,则重新调度重连任务
+// } else {
+// reconnecting = false; // 重连结束后设置 reconnecting 为 false
+// }
+// }
+// }
+// }
+//
+// private void scheduleReconnect() {
+// if (!executorService.isShutdown()) {
+// executorService.schedule(this::attemptReconnect, 5, TimeUnit.SECONDS);
+// }
+// }
private void sendPing() {
try {
--
Gitblit v1.9.3