1
zj
2024-09-29 8bb76f405fb9e5ee135231618c7da357946dc2f8
src/main/java/com/nq/ws/WebsocketRunClient.java
@@ -9,13 +9,21 @@
//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.util.Map;
//import java.nio.charset.StandardCharsets;
//import java.util.*;
//
//@Slf4j
//public class WebsocketRunClient  extends WebSocketClient {
@@ -28,24 +36,33 @@
//        this.eStockType = eStockType;
//    }
//
//    private static HttpClient httpClient = HttpClients.createDefault(); // 单例化 HttpClient
//
//
//    private static HttpPost httpPost;
//    static {
//        httpPost = new HttpPost("http://127.0.0.1:8001/api/sendNotification"); // 初始化 HttpPost
//    }
//
//    @Override
//    public void onOpen(ServerHandshake serverHandshake) {
//        send(("key:"+ eStockType.getStockKey()+":"+eStockType.getContryId()).getBytes());
//        Timer heartbeatTimer;
//        // 启动心跳定时器
//        heartbeatTimer = new Timer();
//        heartbeatTimer.schedule(new TimerTask() {
//            @Override
//            public void run() {
//                send(("key:"+ eStockType.getStockKey()+":"+eStockType.getContryId()).getBytes());
//            }
//        }, 0, 3000); // 每3秒发送一次心跳消息
//    }
//
//    @Override
//    public void onMessage(String s) {
//        Map<String, Object> map = jsonToMap(s);
//        if(map.get("pid").equals("00000001")){
//            System.out.println(s);
//        }
//        try {
//            ApplicationContext act = ApplicationContextRegisterUtil.getApplicationContext();
//            MandatoryLiquidationService liquidationService = (MandatoryLiquidationService) act.getBean(IMandatoryLiquidationService.class);
//        if(!s.equals("pong") && !s.equals("身份验证成功")){
//            StockRealTimeBean stockDetailBean =  new Gson().fromJson(s, StockRealTimeBean.class);
//            liquidationService.RealTimeDataProcess(eStockType,stockDetailBean);
//        }catch (Exception e){
//
//            RedisKeyUtil.setCacheRealTimeStock(eStockType,stockDetailBean);
//        }
//    }
//
@@ -57,11 +74,31 @@
//
//    @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 错误");
//    }
//
//    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);
//        }
//    }
//}