From d9b3fe860d58c8a9759fa7f690febfc8cfc75827 Mon Sep 17 00:00:00 2001
From: Administrator <15274802129@163.com>
Date: Sat, 13 Dec 2025 22:10:21 +0800
Subject: [PATCH] refactor(okxWs): 优化账户和持仓状态更新逻辑

---
 src/main/java/com/xcong/excoin/modules/okxNewPrice/OkxQuantWebSocketClient.java |   42 +++++++++++++++++++++++++++++++++++++-----
 1 files changed, 37 insertions(+), 5 deletions(-)

diff --git a/src/main/java/com/xcong/excoin/modules/okxNewPrice/OkxQuantWebSocketClient.java b/src/main/java/com/xcong/excoin/modules/okxNewPrice/OkxQuantWebSocketClient.java
index 69a0572..3687ea6 100644
--- a/src/main/java/com/xcong/excoin/modules/okxNewPrice/OkxQuantWebSocketClient.java
+++ b/src/main/java/com/xcong/excoin/modules/okxNewPrice/OkxQuantWebSocketClient.java
@@ -273,7 +273,7 @@
                     log.error("WebSocket登录失败, code: {}, msg: {}", code, response.getString("msg"));
                 }
             } else if ("subscribe".equals(event)) {
-                log.info("订阅成功: {}", response.getJSONObject("arg"));
+                subscribeEvent(response);
             } else if ("error".equals(event)) {
                 log.error("订阅错误: code={}, msg={}",
                          response.getString("code"), response.getString("msg"));
@@ -288,6 +288,29 @@
         }
     }
 
+    private void subscribeEvent(JSONObject response) {
+        JSONObject arg = response.getJSONObject("arg");
+        if (arg == null) {
+            log.warn("无效的推送数据,缺少 'arg' 字段 :{}",response);
+            return;
+        }
+
+        String channel = arg.getString("channel");
+        if (channel == null) {
+            log.warn("无效的推送数据,缺少 'channel' 字段{}",response);
+            return;
+        }
+        if (OrderInfoWs.ORDERINFOWS_CHANNEL.equals(channel)) {
+            OrderInfoWs.initEvent(response);
+        }
+        if (AccountWs.ACCOUNTWS_CHANNEL.equals(channel)) {
+            AccountWs.initEvent(response);
+        }
+        if (PositionsWs.POSITIONSWS_CHANNEL.equals(channel)) {
+            PositionsWs.initEvent(response);
+        }
+    }
+
     /**
      * 解析并处理价格推送数据。
      * 将最新的标记价格存入 Redis 并触发后续业务逻辑比较处理。
@@ -295,15 +318,24 @@
      * @param response 包含价格数据的 JSON 对象
      */
     private void processPushData(JSONObject response) {
+        String op = response.getString("op");
+        if (op != null){
+            if (TradeOrderWs.ORDERWS_CHANNEL.equals(op)) {
+                // 直接使用Object类型接收,避免强制类型转换
+                Object data = response.get("data");
+                log.info("收到下单推送结果: {}", JSON.toJSONString(data));
+                return;
+            }
+        }
         JSONObject arg = response.getJSONObject("arg");
         if (arg == null) {
-            log.warn("无效的推送数据,缺少 'arg' 字段");
+            log.warn("无效的推送数据,缺少 'arg' 字段 :{}",response);
             return;
         }
 
         String channel = arg.getString("channel");
         if (channel == null) {
-            log.warn("无效的推送数据,缺少 'channel' 字段");
+            log.warn("无效的推送数据,缺少 'channel' 字段{}",response);
             return;
         }
 
@@ -311,13 +343,13 @@
             OrderInfoWs.handleEvent(response, redisUtils);
         }else if (AccountWs.ACCOUNTWS_CHANNEL.equals(channel)) {
             AccountWs.handleEvent(response);
-            String side = caoZuoService.caoZuo();
-            TradeOrderWs.orderEvent(webSocketClient, side);
         } else if (PositionsWs.POSITIONSWS_CHANNEL.equals(channel)) {
             PositionsWs.handleEvent(response);
         } else if (BalanceAndPositionWs.CHANNEL_NAME.equals(channel)) {
             BalanceAndPositionWs.handleEvent(response);
         }
+        String side = caoZuoService.caoZuo();
+        TradeOrderWs.orderEvent(webSocketClient, side);
     }
 
     /**

--
Gitblit v1.9.1