1
zyy3
2025-12-02 d88d0f6ce31efb978de10227f6a12e4cc4843129
trading-order-huobi/src/main/java/com/yami/trading/huobi/jsws/WebsocketRunClient.java
@@ -4,11 +4,16 @@
import com.google.gson.reflect.TypeToken;
import com.yami.trading.bean.data.domain.Realtime;
import com.yami.trading.bean.item.domain.Item;
import com.yami.trading.huobi.data.DataCache;
import com.yami.trading.huobi.websocket.constant.enums.EStockType;
import com.yami.trading.service.item.ItemService;
import lombok.extern.slf4j.Slf4j;
import org.java_websocket.client.WebSocketClient;
import org.java_websocket.handshake.ServerHandshake;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Component;
import java.lang.reflect.Type;
import java.math.BigDecimal;
@@ -17,20 +22,29 @@
import java.util.*;
@Slf4j
@Component
@Scope("prototype")
public class WebsocketRunClient extends WebSocketClient {
    private EStockType eStockType;
    @Autowired
    ItemService itemService;
    public WebsocketRunClient() {
        super(URI.create(EStockType.US.wsUrl));
    }
    public WebsocketRunClient(URI serverUri, EStockType eStockType) {
        // 修改为新的WebSocket服务器地址
        super(URI.create("wss://usws.yanshiz.com/websocket-server"));
        super(URI.create(eStockType.wsUrl));
        this.eStockType = eStockType;
    }
    @Override
    public void onOpen(ServerHandshake serverHandshake) {
        log.info("WebSocket连接已建立,连接到: wss://usws.yanshiz.com/websocket-server");
        log.info("WebSocket连接已建立,连接到: {}", EStockType.US.wsUrl);
        // 发送身份验证消息
        send(("key:"+ eStockType.getStockKey()+":"+eStockType.getContryId()).getBytes());
        Timer heartbeatTimer;
@@ -52,8 +66,28 @@
        if(!s.equals("pong") && !s.equals("身份验证成功")){
            try {
                Map<String, String> stringObjectMap = jsonToMap(s);
                String symbol = stringObjectMap.get("Symbol").trim();
                Item item = itemService.findCaCheBySymbol(symbol);
                if (item == null || !item.getType().equalsIgnoreCase(Item.US_STOCKS)) {
                    return;
                }
                System.out.println(s);
                /*Map<String, String> stringObjectMap = jsonToMap(s);
                StockRealTimeBean stockRealTimeBean = new StockRealTimeBean();
                stockRealTimeBean.setPid(stringObjectMap.get("Id").toString());
                stockRealTimeBean.setLast(stringObjectMap.get("Last").toString());
                stockRealTimeBean.setBid(stringObjectMap.get("Bid").toString());
                stockRealTimeBean.setAsk(stringObjectMap.get("Ask").toString());
                stockRealTimeBean.setHigh(stringObjectMap.get("High").toString());
                stockRealTimeBean.setLow(stringObjectMap.get("Low").toString());
                stockRealTimeBean.setPc(stringObjectMap.get("Chg").toString());
                stockRealTimeBean.setPcp(stringObjectMap.get("ChgPct").toString()+"%");
                stockRealTimeBean.setTime(stringObjectMap.get("Time").toString());
                stockRealTimeBean.setOpen(stringObjectMap.get("Open"));
                stockRealTimeBean.setPrevClose(stringObjectMap.get("PrevClose"));
                stockRealTimeBean.setSymbol(stringObjectMap.get("Symbol"));*/
                Realtime realtime = new Realtime();
                realtime.setUuid(stringObjectMap.get("pid"));
                /*realtime.setUuid(stringObjectMap.get("pid"));
                realtime.setSymbol(stringObjectMap.get("symbol"));
                realtime.setClose(new BigDecimal(stringObjectMap.get("last")).doubleValue());
                realtime.setLow(new BigDecimal(stringObjectMap.get("low")).doubleValue());
@@ -64,10 +98,24 @@
                realtime.setNetChange(new BigDecimal(stringObjectMap.get("pc")).doubleValue());
                realtime.setChangeRatio(parsePercent(stringObjectMap.get("pcp")));
                realtime.setBid(new BigDecimal(stringObjectMap.get("bid")).doubleValue());
                realtime.setAsk(new BigDecimal(stringObjectMap.get("ask")).doubleValue());
                realtime.setAsk(new BigDecimal(stringObjectMap.get("ask")).doubleValue());*/
                realtime.setUuid(stringObjectMap.get("Id"));
                realtime.setSymbol(stringObjectMap.get("Symbol"));
                realtime.setClose(new BigDecimal(stringObjectMap.get("Last")).doubleValue());
                realtime.setLow(new BigDecimal(stringObjectMap.get("Low")).doubleValue());
                realtime.setHigh(new BigDecimal(stringObjectMap.get("High")).doubleValue());
                realtime.setOpen(new BigDecimal(stringObjectMap.get("Open")).doubleValue());
                realtime.setPrevClose(new BigDecimal(stringObjectMap.get("PrevClose")).doubleValue());
                realtime.setTs(Long.valueOf(stringObjectMap.get("Time") + "000"));
                realtime.setNetChange(new BigDecimal(stringObjectMap.get("Chg")).doubleValue());
                realtime.setChangeRatio(new BigDecimal(stringObjectMap.get("ChgPct")).doubleValue());
                realtime.setBid(new BigDecimal(stringObjectMap.get("Bid")).doubleValue());
                realtime.setAsk(new BigDecimal(stringObjectMap.get("Ask")).doubleValue());
                DataCache.putRealtime(realtime.getSymbol(), realtime);
            } catch (Exception e) {
                log.error("处理WebSocket消息时发生错误: {}", e.getMessage(), e);
                log.error("处理WebSocket消息时发生错误: {}", e);
            }
        } else {
            log.info("WebSocket心跳或认证响应: {}", s);
@@ -117,4 +165,20 @@
            throw new IllegalArgumentException("无效的百分比格式:" + percentStr, e);
        }
    }
    public boolean verify(String str) {
        if (str == null) {
            return false;
        }
        if (str.trim().isEmpty()) {
            return false;
        }
        if (str.trim().equals("undefined")) {
            return false;
        }
        if (str.trim().equals("null")) {
            return false;
        }
        return true;
    }
}