1
dd
2025-12-26 3cfb3d987566f4baf5563a52dced85c9bd8a5391
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
package com.nq.ws;
 
import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;
import com.nq.enums.EStockType;
import com.nq.pojo.StockRealTimeBean;
import com.nq.service.IMandatoryLiquidationService;
import com.nq.service.impl.MandatoryLiquidationService;
import com.nq.utils.ApplicationContextRegisterUtil;
import com.nq.utils.redis.RedisKeyUtil;
import lombok.extern.slf4j.Slf4j;
import org.java_websocket.client.WebSocketClient;
import org.java_websocket.handshake.ServerHandshake;
import org.springframework.context.ApplicationContext;
 
import java.lang.reflect.Type;
import java.net.URI;
import java.util.HashMap;
import java.util.Map;
 
@Slf4j
public class WebsocketRunClient extends WebSocketClient {
 
    private EStockType eStockType;
 
    public WebsocketRunClient(URI serverUri, EStockType eStockType) {
        super(serverUri);
        this.eStockType = eStockType;
    }
 
    @Override
    public void onOpen(ServerHandshake serverHandshake) {
        log.info("WebSocket连接已建立,服务器: {}", getURI());
 
        // 根据新服务器的协议格式发送认证或订阅消息
        String subscribeMessage = buildSubscribeMessage();
        send(subscribeMessage);
        log.info("已发送订阅消息: {}", subscribeMessage);
    }
 
    @Override
    public void onMessage(String message) {
        try {
            log.debug("收到WebSocket消息: {}", message);
 
            ApplicationContext act = ApplicationContextRegisterUtil.getApplicationContext();
            MandatoryLiquidationService liquidationService = (MandatoryLiquidationService) act.getBean(IMandatoryLiquidationService.class);
 
            // 根据新服务器的数据格式解析消息
            StockRealTimeBean stockDetailBean = parseMessage(message);
            if (stockDetailBean != null) {
                liquidationService.RealTimeDataProcess(eStockType, stockDetailBean);
                RedisKeyUtil.setCacheRealTimeStock(eStockType, stockDetailBean);
            }
 
        } catch (Exception e) {
            log.error("处理WebSocket消息时发生错误: {}", e.getMessage(), e);
        }
    }
 
    @Override
    public void onClose(int code, String reason, boolean remote) {
        log.info("WebSocket连接关闭 - 代码: {}, 原因: {}, 远程关闭: {}", code, reason, remote);
        log.info("股票类型: {}", eStockType);
    }
 
    @Override
    public void onError(Exception e) {
        log.error("WebSocket连接错误: {}", e.getMessage(), e);
    }
 
    /**
     * 构建订阅消息 - 根据新服务器的协议格式调整
     */
    private String buildSubscribeMessage() {
        // 根据新服务器的API文档调整消息格式
        // 示例格式,需要根据实际API调整
        // 使用 HashMap
        Map<String, Object> subscribeMsg = new HashMap<>();
        subscribeMsg.put("action", "subscribe");
        subscribeMsg.put("key", eStockType.getStockKey());
        subscribeMsg.put("country", eStockType.getContryId());
        subscribeMsg.put("type", "stock_realtime");
 
        return new Gson().toJson(subscribeMsg);
    }
 
    /**
     * 解析消息 - 根据新服务器的数据格式调整
     */
    private StockRealTimeBean parseMessage(String message) {
        try {
            // 首先将消息解析为Map查看结构
            Map<String, Object> messageMap = jsonToMap(message);
            log.debug("消息结构: {}", messageMap);
 
            // 根据实际的数据结构进行转换
            // 这里需要根据新服务器的数据格式调整解析逻辑
 
            // 如果是直接符合StockRealTimeBean结构的消息
            return new Gson().fromJson(message, StockRealTimeBean.class);
 
        } catch (Exception e) {
            log.error("解析消息失败: {}, 原始消息: {}", e.getMessage(), message);
            return null;
        }
    }
 
    public static Map<String, Object> jsonToMap(String json) {
        Gson gson = new Gson();
        Type type = new TypeToken<Map<String, Object>>(){}.getType();
        return gson.fromJson(json, type);
    }
}