1
zj
2024-09-10 1c6748cbef6bf2348106b27fb47a74efff8a8b2e
src/main/java/com/nq/ws/WebsocketRunClient.java
@@ -1,6 +1,7 @@
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;
@@ -8,45 +9,98 @@
import com.nq.utils.ApplicationContextRegisterUtil;
import com.nq.utils.redis.RedisKeyUtil;
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.context.ApplicationContext;
import java.io.IOException;
import java.lang.reflect.Type;
import java.net.URI;
import java.nio.charset.StandardCharsets;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
@Slf4j
public class WebsocketRunClient  extends WebSocketClient {
    public WebsocketRunClient(URI serverUri) {
    private EStockType eStockType;
    public WebsocketRunClient(URI serverUri,
    EStockType eStockType
    ) {
        super(serverUri);
        this.eStockType = eStockType;
    }
    @Override
    public void onOpen(ServerHandshake serverHandshake) {
        send(("key:"+ EStockType.IN.getStockKey()+":"+EStockType.IN.getContryId()).getBytes());
        send(("key:"+ eStockType.getStockKey()+":"+eStockType.getContryId()+":v1").getBytes());
    }
    @Override
    public void onMessage(String s) {
        try {
       try {
           if(!s.equals("pong") && !s.equals("身份验证成功")){
               StockRealTimeBean stockDetailBean =  new Gson().fromJson(s, StockRealTimeBean.class);
               RedisKeyUtil.setCacheRealTimeStock(eStockType,stockDetailBean);
               send(s);
           }
       }catch (Exception e){
           log.error("ws 消息接收错误:"+s);
       }
    }
            ApplicationContext act = ApplicationContextRegisterUtil.getApplicationContext();
            MandatoryLiquidationService liquidationService = (MandatoryLiquidationService) act.getBean(IMandatoryLiquidationService.class);
            StockRealTimeBean stockDetailBean =  new Gson().fromJson(s, StockRealTimeBean.class);
            liquidationService.RealTimeDataProcess(EStockType.IN,stockDetailBean);
        }catch (Exception e){
        }
    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);
    }
    @Override
    public void onClose(int i, String s, boolean b) {
        log.info("websocket 关闭"+1);
        log.info("websocket  日股  关闭"+1);
    }
    @Override
    public void onError(Exception e) {
        log.info("websocket 错误");
    }
    private HttpClient httpClient;  //  声明一个成员变量
    private HttpPost httpPost = new HttpPost("http://127.0.0.1:8001/api/sendNotification");  //  创建一个HttpPost对象
    @Override
    public void send(String message) {
        try {
            if (httpClient == null) {
                httpClient = HttpClients.createDefault();  //  使用单例模式创建HttpClient对象
            }
            //  准备  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) {
            e.printStackTrace();
        }
    }
}