1
zyy
2025-12-02 25d9d0e85eb9bd75bcf5296e77012b4873e31215
trading-order-huobi/src/main/java/com/yami/trading/huobi/jsws/WebsocketRunClient.java
@@ -1,30 +1,48 @@
package com.yami.trading.huobi.jsws;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.google.gson.Gson;
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.bean.model.StockWSBean;
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.apache.http.HttpResponse;
import org.apache.http.client.HttpClient;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.message.BasicNameValuePair;
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.io.IOException;
import java.lang.reflect.Type;
import java.math.BigDecimal;
import java.math.RoundingMode;
import java.net.URI;
import java.nio.charset.StandardCharsets;
import java.util.*;
@Slf4j
@Component
@Scope("prototype")
public class WebsocketRunClient extends WebSocketClient {
    private static HttpClient httpClient = HttpClients.createDefault(); // 单例化 HttpClient
    private static HttpPost httpPost;
    static {
        httpPost = new HttpPost("http://127.0.0.1:8001/api/sendNotification"); // 初始化 HttpPost
    }
    private EStockType eStockType;
@@ -72,20 +90,19 @@
                    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());
                StockWSBean stockRealTimeBean = new StockWSBean();
                stockRealTimeBean.setPid(stringObjectMap.get("Id"));
                stockRealTimeBean.setLast(stringObjectMap.get("Last"));
                stockRealTimeBean.setBid(stringObjectMap.get("Bid"));
                stockRealTimeBean.setAsk(stringObjectMap.get("Ask"));
                stockRealTimeBean.setHigh(stringObjectMap.get("High"));
                stockRealTimeBean.setLow(stringObjectMap.get("Low"));
                stockRealTimeBean.setPc(stringObjectMap.get("Chg"));
                stockRealTimeBean.setPcp(stringObjectMap.get("ChgPct") +"%");
                stockRealTimeBean.setTime(stringObjectMap.get("Time"));
                stockRealTimeBean.setOpen(stringObjectMap.get("Open"));
                stockRealTimeBean.setPrevClose(stringObjectMap.get("PrevClose"));
                stockRealTimeBean.setSymbol(stringObjectMap.get("Symbol"));*/
                stockRealTimeBean.setSymbol(stringObjectMap.get("Symbol"));
                Realtime realtime = new Realtime();
                /*realtime.setUuid(stringObjectMap.get("pid"));
                realtime.setSymbol(stringObjectMap.get("symbol"));
@@ -114,6 +131,11 @@
                DataCache.putRealtime(realtime.getSymbol(), realtime);
                ObjectMapper objectMapper = new ObjectMapper();
                String json = objectMapper.writeValueAsString(stockRealTimeBean);
                sendLoca(json);
            } catch (Exception e) {
                log.error("处理WebSocket消息时发生错误: {}", e);
            }
@@ -181,4 +203,24 @@
        }
        return true;
    }
    public void sendLoca(String message) {
        try {
            // 准备 form-data 参数
            List<BasicNameValuePair> params = new ArrayList<>();
            params.add(new BasicNameValuePair("message", message));
            // 设置编码格式为 UTF-8
            UrlEncodedFormEntity entity = new UrlEncodedFormEntity(params, StandardCharsets.UTF_8);
            httpPost.setEntity(entity); // 设置 HttpPost 对象的参数
            // 发送请求
            HttpResponse response = httpClient.execute(httpPost);
            // 处理响应
            int statusCode = response.getStatusLine().getStatusCode();
        } catch (IOException e) {
            log.error("Http 请求错误", e);
        }
    }
}