1
zj
2024-09-19 c8f0a9bda343a7e99d1b10db9efdc2cf3ad2f525
src/main/java/com/nq/ws/WsClientConfig.java
@@ -35,6 +35,9 @@
import java.util.List;
import java.util.Map;
/**
 * soket-io
 */
@Slf4j
@Configuration
public class WsClientConfig {
@@ -44,6 +47,13 @@
    private static final String ROOM_ID = "14";
    private static Socket socket;
    private static HttpClient httpClient = HttpClients.createDefault(); // 单例化 HttpClient
    private static HttpPost httpPost;
    static {
        httpPost = new HttpPost("http://127.0.0.1:8001/api/sendNotification"); // 初始化 HttpPost
    }
    @Bean
    public void websocketRunClientMap() {
        connectToServer();
@@ -71,18 +81,7 @@
        socket.on("marketData", new Emitter.Listener() {
            @Override
            public void call(Object... args) {
                    Map<String, Object> map = jsonToMap(args[0].toString());
                    if(map.get("pid").equals("00000001")){
                        System.out.println("接收时间:"  +  new SimpleDateFormat("HH:mm:ss").format(new  Date())  +  "  "  +  args[0].toString());
                    }
                try {
                    ApplicationContext act = ApplicationContextRegisterUtil.getApplicationContext();
                    MandatoryLiquidationService liquidationService = (MandatoryLiquidationService) act.getBean(IMandatoryLiquidationService.class);
                    StockRealTimeBean stockDetailBean =  new Gson().fromJson(args[0].toString(), StockRealTimeBean.class);
                    liquidationService.RealTimeDataProcess(EStockType.IN,stockDetailBean);
                }catch (Exception e){
                    log.error("socket数据存入缓存错误:", e.getMessage());
                }
                send(args[0].toString());
            }
        });
@@ -144,4 +143,24 @@
            }
        }
    }
    private static void send(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);
        }
    }
}