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
---
geteClient/src/main/java/org/example/geteclient/wsClinet/GateClient.java | 121 +++++++++++++++++++++++++---------------
1 files changed, 76 insertions(+), 45 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 94024f1..3301db2 100644
--- a/geteClient/src/main/java/org/example/geteclient/wsClinet/GateClient.java
+++ b/geteClient/src/main/java/org/example/geteclient/wsClinet/GateClient.java
@@ -6,11 +6,19 @@
import com.google.gson.Gson;
import com.google.gson.JsonSyntaxException;
import lombok.extern.slf4j.Slf4j;
+import org.example.geteclient.GeteClientApplication;
+import org.example.geteclient.comm.ApplicationContextProvider;
import org.example.geteclient.pojo.Currency;
import org.example.geteclient.util.RedisUtil;
import org.json.JSONException;
+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.math.BigDecimal;
import java.net.URI;
import java.nio.ByteBuffer;
import java.util.Arrays;
@@ -45,7 +53,7 @@
this.executorService = Executors.newScheduledThreadPool(1); // 创建一个定时任务调度线程池
}
- public void start() {
+ public void start() throws Exception {
try {
connect(); // 尝试连接 WebSocket
if (session == null) { // 如果连接失败,session 仍然为 null
@@ -68,6 +76,7 @@
} catch (Exception e) {
log.error("gate ws 连接过程中发生异常: " + e.getMessage(), e); // 记录连接过程中发生的异常
+ throw e;
} finally {
executorService.shutdown(); // 确保定时任务调度服务被关闭
}
@@ -104,12 +113,33 @@
if (resultMap != null && resultMap.get(S_KEY) != null) {
HashMap<String, Object> hashMap = new HashMap<>(); // 创建新的 HashMap 保存 bids 和 asks
- // 放入 bids 和 asks 数据
- hashMap.put(BIDS_KEY, resultMap.get(BIDS_KEY)); // 放入 bids 数据
- hashMap.put(ASKS_KEY, resultMap.get(ASKS_KEY)); // 放入 asks 数据
+ Object asksObj = resultMap.get(BIDS_KEY);
+ Object bidsObj = resultMap.get(ASKS_KEY);
- String key = "gate" + resultMap.get(S_KEY); // 生成 Redis 键
- RedisUtil.set(key.replace("_", ""), gson.toJson(hashMap)); // 存入 Redis,使用 Gson 进行序列化
+ if(asksObj instanceof List && !((List<?>) asksObj).isEmpty() && bidsObj instanceof List && !((List<?>) bidsObj).isEmpty()){
+ if (asksObj instanceof List && !((List<?>) asksObj).isEmpty()) {
+ List<?> asksList = (List<?>) asksObj;
+ String[][] dataArray = gson.fromJson(gson.toJson(asksList), String[][].class);
+ HashMap<String, Object> pvMap = new HashMap<>(); // 创建新的 HashMap 保存 bids 和 asks
+ String[] asksData = dataArray[0];
+ pvMap.put("p", new BigDecimal(asksData[0]).toPlainString());
+ pvMap.put("v", new BigDecimal(asksData[1]).toPlainString());
+ hashMap.put(ASKS_KEY, pvMap); // 放入 bids 数据
+ }
+
+ if (bidsObj instanceof List && !((List<?>) bidsObj).isEmpty()) {
+ List<?> bidsList = (List<?>) bidsObj;
+ String[][] dataArray = gson.fromJson(gson.toJson(bidsList), String[][].class);
+ String[] bidsData = dataArray[0];
+ HashMap<String, Object> pvMap = new HashMap<>(); // 创建新的 HashMap 保存 bids 和 asks
+ pvMap.put("p", new BigDecimal(bidsData[0]).toPlainString());
+ pvMap.put("v", new BigDecimal(bidsData[1]).toPlainString());
+ hashMap.put(BIDS_KEY,pvMap);
+ }
+
+ String key = "gate" + resultMap.get(S_KEY); // 生成 Redis 键
+ RedisUtil.set(key.replace("_", ""), gson.toJson(hashMap)); // 存入 Redis,使用 Gson 进行序列化
+ }
}
} catch (JsonSyntaxException e) {
log.error("JSON 解析异常:" + e.getMessage(), e); // 记录 JSON 解析异常
@@ -119,52 +149,53 @@
}
+
@OnClose
- public void onClose() {
+ public void onClose() throws Exception {
log.info("gate ws 连接已关闭,尝试重新连接..."); // 连接关闭日志
- handleConnectionClosedOrError(); // 处理连接关闭或错误
+ throw new Exception();
}
@OnError
- public void onError(Throwable throwable) {
+ public void onError(Throwable throwable) throws Exception {
log.error("gate 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("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 {
@@ -189,7 +220,7 @@
final String CHANNEL = "spot.order_book"; // 固定频道名称
final String EVENT_SUBSCRIBE = "subscribe"; // 订阅事件
final String EVENT_UNSUBSCRIBE = "unsubscribe"; // 取消订阅事件
- final String[] PAYLOAD = new String[]{symbol, "20", "100ms"}; // 请求负载信息
+ final String[] PAYLOAD = new String[]{symbol, "5", "100ms"}; // 请求负载信息
// 使用Map构建JSON对象
Map<String, Object> jsonMap = new HashMap<>(); // 创建 Map 存放 JSON 内容
--
Gitblit v1.9.3