From 3134ce0036412b382b4ffacee73a1e2f7ba298bb Mon Sep 17 00:00:00 2001
From: zj <1772600164@qq.com>
Date: Sun, 30 Jun 2024 20:19:59 +0800
Subject: [PATCH] 1

---
 src/main/java/com/nq/ws/WsClientConfig.java |  296 +++++++++++++++++++++++++++++-----------------------------
 1 files changed, 149 insertions(+), 147 deletions(-)

diff --git a/src/main/java/com/nq/ws/WsClientConfig.java b/src/main/java/com/nq/ws/WsClientConfig.java
index 61a2f51..c741946 100644
--- a/src/main/java/com/nq/ws/WsClientConfig.java
+++ b/src/main/java/com/nq/ws/WsClientConfig.java
@@ -1,147 +1,149 @@
-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;
-import com.nq.service.impl.MandatoryLiquidationService;
-import com.nq.utils.ApplicationContextRegisterUtil;
-import com.nq.utils.PropertiesUtil;
-import com.nq.ws.client.IO;
-import com.nq.ws.client.Socket;
-import io.socket.emitter.Emitter;
-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.json.JSONException;
-import org.json.JSONObject;
-import org.springframework.context.ApplicationContext;
-import org.springframework.context.annotation.Bean;
-import org.springframework.context.annotation.Configuration;
-
-import java.io.IOException;
-import java.lang.reflect.Type;
-import java.net.URI;
-import java.net.URISyntaxException;
-import java.nio.charset.StandardCharsets;
-import java.text.SimpleDateFormat;
-import java.util.ArrayList;
-import java.util.Date;
-import java.util.List;
-import java.util.Map;
-
-@Slf4j
-@Configuration
-public class WsClientConfig {
-
-    private static final String SERVER_URL = PropertiesUtil.getProperty("JS_IN_HTTP_API");
-    private static final String AUTH_KEY = PropertiesUtil.getProperty("JS_IN_KEY");
-    private static final String ROOM_ID = "14";
-    private static Socket socket;
-
-    @Bean
-    public void websocketRunClientMap() {
-        connectToServer();
-    }
-
-    private static void connectToServer() {
-        IO.Options options = new IO.Options();
-        options.reconnection = true;
-        options.reconnectionDelay = 1000;
-
-        try {
-            socket = IO.socket(new URI(SERVER_URL), options);
-        } catch (URISyntaxException e) {
-            log.error("Invalid URI", e);
-            return;
-        }
-
-        socket.on(Socket.EVENT_CONNECT, new Emitter.Listener() {
-            @Override
-            public void call(Object... args) {
-                authenticate();
-            }
-        });
-
-        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());
-                }
-            }
-        });
-
-        socket.on(Socket.EVENT_CONNECT_ERROR, new Emitter.Listener() {
-            @Override
-            public void call(Object... args) {
-                log.error("socketIo连接错误: " + args[0]);
-            }
-        });
-
-        socket.on(Socket.EVENT_DISCONNECT, new Emitter.Listener() {
-            @Override
-            public void call(Object... args) {
-                log.error("socketIo 断开连接: " + args[0]);
-                if (!socket.connected()) {
-                    log.error("socketIo 开始重连: " + args[0]);
-                    reconnect();
-                }
-            }
-        });
-
-        socket.connect();
-    }
-
-    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);
-    }
-
-    private static void reconnect() {
-        new Thread(new Runnable() {
-            @Override
-            public void run() {
-                while (!socket.connected()) {
-                    try {
-                        log.error("socketIo 开始重连");
-                        socket.connect();
-                        Thread.sleep(5000); // 重连间隔,单位毫秒
-                    } catch (InterruptedException e) {
-                        log.error("重连被中断", e);
-                    }
-                }
-            }
-        }).start();
-    }
-
-    private static void authenticate() {
-        log.info("socketIo---->开始连接");
-        if (socket != null && socket.connected()) {
-            JSONObject authData = new JSONObject();
-            try {
-                authData.put("key", AUTH_KEY);
-                authData.put("roomId", ROOM_ID);
-                socket.emit("authenticate", authData);
-                log.info("socketIo---->连接成功");
-            } catch (JSONException e) {
-                log.error("socketIo认证错误:"+e.getMessage(), e);
-            }
-        }
-    }
-}
+//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;
+//import com.nq.service.impl.MandatoryLiquidationService;
+//import com.nq.utils.ApplicationContextRegisterUtil;
+//import com.nq.utils.PropertiesUtil;
+//import com.nq.ws.client.IO;
+//import com.nq.ws.client.Socket;
+//import io.socket.emitter.Emitter;
+//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.json.JSONException;
+//import org.json.JSONObject;
+//import org.springframework.context.ApplicationContext;
+//import org.springframework.context.annotation.Bean;
+//import org.springframework.context.annotation.Configuration;
+//
+//import java.io.IOException;
+//import java.lang.reflect.Type;
+//import java.net.URI;
+//import java.net.URISyntaxException;
+//import java.nio.charset.StandardCharsets;
+//import java.text.SimpleDateFormat;
+//import java.util.ArrayList;
+//import java.util.Date;
+//import java.util.List;
+//import java.util.Map;
+/**
+ * soket-io
+ */
+//@Slf4j
+//@Configuration
+//public class WsClientConfig {
+//
+//    private static final String SERVER_URL = PropertiesUtil.getProperty("JS_IN_HTTP_API");
+//    private static final String AUTH_KEY = PropertiesUtil.getProperty("JS_IN_KEY");
+//    private static final String ROOM_ID = "14";
+//    private static Socket socket;
+//
+//    @Bean
+//    public void websocketRunClientMap() {
+//        connectToServer();
+//    }
+//
+//    private static void connectToServer() {
+//        IO.Options options = new IO.Options();
+//        options.reconnection = true;
+//        options.reconnectionDelay = 1000;
+//
+//        try {
+//            socket = IO.socket(new URI(SERVER_URL), options);
+//        } catch (URISyntaxException e) {
+//            log.error("Invalid URI", e);
+//            return;
+//        }
+//
+//        socket.on(Socket.EVENT_CONNECT, new Emitter.Listener() {
+//            @Override
+//            public void call(Object... args) {
+//                authenticate();
+//            }
+//        });
+//
+//        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());
+//                }
+//            }
+//        });
+//
+//        socket.on(Socket.EVENT_CONNECT_ERROR, new Emitter.Listener() {
+//            @Override
+//            public void call(Object... args) {
+//                log.error("socketIo连接错误: " + args[0]);
+//            }
+//        });
+//
+//        socket.on(Socket.EVENT_DISCONNECT, new Emitter.Listener() {
+//            @Override
+//            public void call(Object... args) {
+//                log.error("socketIo 断开连接: " + args[0]);
+//                if (!socket.connected()) {
+//                    log.error("socketIo 开始重连: " + args[0]);
+//                    reconnect();
+//                }
+//            }
+//        });
+//
+//        socket.connect();
+//    }
+//
+//    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);
+//    }
+//
+//    private static void reconnect() {
+//        new Thread(new Runnable() {
+//            @Override
+//            public void run() {
+//                while (!socket.connected()) {
+//                    try {
+//                        log.error("socketIo 开始重连");
+//                        socket.connect();
+//                        Thread.sleep(5000); // 重连间隔,单位毫秒
+//                    } catch (InterruptedException e) {
+//                        log.error("重连被中断", e);
+//                    }
+//                }
+//            }
+//        }).start();
+//    }
+//
+//    private static void authenticate() {
+//        log.info("socketIo---->开始连接");
+//        if (socket != null && socket.connected()) {
+//            JSONObject authData = new JSONObject();
+//            try {
+//                authData.put("key", AUTH_KEY);
+//                authData.put("roomId", ROOM_ID);
+//                socket.emit("authenticate", authData);
+//                log.info("socketIo---->连接成功");
+//            } catch (JSONException e) {
+//                log.error("socketIo认证错误:"+e.getMessage(), e);
+//            }
+//        }
+//    }
+//}

--
Gitblit v1.9.3