From 9ed28183ca391eadb7b5cb2f3160e3ec852da124 Mon Sep 17 00:00:00 2001
From: zj <1772600164@qq.com>
Date: Fri, 11 Oct 2024 23:25:21 +0800
Subject: [PATCH] 1

---
 bitgetsClient/src/main/java/org/example/bitgetsclient/wsClient/BitgetClient.java |   71 +++++++++++++++++++++++++----------
 1 files changed, 51 insertions(+), 20 deletions(-)

diff --git a/bitgetsClient/src/main/java/org/example/bitgetsclient/wsClient/BitgetClient.java b/bitgetsClient/src/main/java/org/example/bitgetsclient/wsClient/BitgetClient.java
index b9c61a8..dcdb2b1 100644
--- a/bitgetsClient/src/main/java/org/example/bitgetsclient/wsClient/BitgetClient.java
+++ b/bitgetsClient/src/main/java/org/example/bitgetsclient/wsClient/BitgetClient.java
@@ -8,13 +8,22 @@
 import com.google.gson.Gson;
 import com.google.gson.JsonSyntaxException;
 import lombok.extern.slf4j.Slf4j;
+import org.example.bitgetsclient.BitgetsClientApplication;
+import org.example.bitgetsclient.comm.ApplicationContextProvider;
 import org.example.bitgetsclient.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.context.annotation.Bean;
+import org.springframework.stereotype.Component;
 import org.springframework.util.CollectionUtils;
 
 import javax.websocket.*;
 import java.io.IOException;
+import java.lang.reflect.Type;
+import java.math.BigDecimal;
 import java.net.URI;
 import java.nio.ByteBuffer;
 import java.util.Arrays;
@@ -24,6 +33,7 @@
 import java.util.concurrent.Executors;
 import java.util.concurrent.ScheduledExecutorService;
 import java.util.concurrent.TimeUnit;
+import java.util.concurrent.atomic.AtomicInteger;
 
 @ClientEndpoint
 @Slf4j
@@ -53,7 +63,7 @@
 
 
 
-    public void start() {
+    public void start()  {
         try {
             connect(); // 尝试连接
             if (session == null) {
@@ -65,7 +75,6 @@
             executorService.scheduleAtFixedRate(this::sendPing, PING_INTERVAL, PING_INTERVAL, TimeUnit.MILLISECONDS);
             // 发送订阅消息
             session.getBasicRemote().sendText(subscriptions); // 发送订阅信息
-
             synchronized (this) {
                 this.wait(); // 等待 WebSocket 消息到来
             }
@@ -105,17 +114,39 @@
 
                 // 确保 dataNode 不为 null,以避免空指针异常
                 if (dataNode != null) {
-                    // 存储数据到 HashMap
-                    Map<String, Object> bidAskMap = new HashMap<>(); // 变量命名更具描述性
-                    bidAskMap.put("bids", dataNode.get("bids")); // 获取并存储 bids
-                    bidAskMap.put("asks", dataNode.get("asks")); // 获取并存储 asks
+                    Map<String, Object> hashMap = new HashMap<>(); // 变量命名更具描述性
 
-                    Map<String, Object> argMap = gson.fromJson(map.get("arg").toString(), new TypeToken<Map<String, Object>>() {}.getType()); // 解析 arg 为 Map
-                    String key = "bitget" + argMap.get("instId"); // 构建 Redis 存储的键
+                    String asks = dataNode.get("bids").toString();
+                    String bids = dataNode.get("asks").toString();
 
-                    // 存储到 Redis,使用 ObjectMapper 转换为 JSON 字符串
-                    String jsonData = objectMapper.writeValueAsString(bidAskMap); // 先将 HashMap 转换为 JSON 字符串
-                    RedisUtil.set(key, jsonData); // 存储数据
+                    Type listType = new TypeToken<List<List<String>>>(){}.getType();
+                    List<List<String>> bidsList = gson.fromJson(bids, listType);
+                    List<List<String>> asksList = gson.fromJson(asks, listType);
+
+                    if(!bidsList.isEmpty()  && !bidsList.get(0).isEmpty() && !asksList.isEmpty() && !asksList.get(0).isEmpty()){
+                        if (!bidsList.isEmpty()  && !bidsList.get(0).isEmpty()) {
+                            List<String> bidsStringList = bidsList.get(0);
+                            HashMap<String, Object> pvMap = new HashMap<>(); // 创建新的 HashMap 保存 bids 和 asks
+                            pvMap.put("p", new BigDecimal(bidsStringList.get(0)).toPlainString());
+                            pvMap.put("v", new BigDecimal(bidsStringList.get(1)).toPlainString());
+                            hashMap.put("bids", pvMap); // 获取并存储 bids
+                        }
+
+                        if (!asksList.isEmpty() && !asksList.get(0).isEmpty()) {
+                            List<String> asksStringList = asksList.get(0); // 修正此处为 asksList
+                            HashMap<String, Object> pvMap = new HashMap<>(); // 创建新的 HashMap 保存 bids 和 asks
+                            pvMap.put("p", new BigDecimal(asksStringList.get(0)).toPlainString());
+                            pvMap.put("v", new BigDecimal(asksStringList.get(1)).toPlainString());
+                            hashMap.put("asks", pvMap); // 获取并存储 asks
+                        }
+
+                        Map<String, Object> argMap = gson.fromJson(map.get("arg").toString(), new TypeToken<Map<String, Object>>() {}.getType()); // 解析 arg 为 Map
+                        String key = "bitget" + argMap.get("instId"); // 构建 Redis 存储的键
+
+                        // 存储到 Redis,使用 ObjectMapper 转换为 JSON 字符串
+                        String jsonData = objectMapper.writeValueAsString(hashMap); // 先将 HashMap 转换为 JSON 字符串
+                        RedisUtil.set(key, jsonData); // 存储数据
+                    }
                 }
             }
         } catch (JsonSyntaxException e) {
@@ -143,24 +174,25 @@
     }
 
     @OnClose
-    public void onClose() {
+    public void onClose()  {
         log.info("bitget ws 连接已关闭,尝试重新连接..."); // 记录连接关闭的信息
-        handleConnectionClosedOrError(); // 处理连接关闭事件
+        handleConnectionClosedOrError();
     }
 
     @OnError
     public void onError(Throwable throwable) {
         log.error("bitget ws 发生错误: " + throwable.getMessage(), throwable); // 记录错误信息
-        handleConnectionClosedOrError(); // 处理错误事件
+        handleConnectionClosedOrError();
     }
 
     private void handleConnectionClosedOrError() {
-        synchronized (lock) {
-            if (!reconnecting) {
-                reconnecting = true; // 开始重连
-                executorService.execute(this::attemptReconnect); // 执行重连操作
+        executorService.execute(() -> {
+            try {
+                attemptReconnect();
+            } catch (Exception e) {
+                throw new RuntimeException(e);
             }
-        }
+        });
     }
 
     private void attemptReconnect() {
@@ -179,7 +211,6 @@
             }
         } else {
             log.error("超过最大重连次数,停止重连"); // 超过最大重连次数
-            reconnecting = false; // 重连状态重置
         }
     }
 

--
Gitblit v1.9.3