From fe3daa02bf1ca618d2eb941d82d96e09fa933409 Mon Sep 17 00:00:00 2001
From: zyy <zyy@email.com>
Date: Fri, 25 Jul 2025 16:20:51 +0800
Subject: [PATCH] ws

---
 src/main/resources/application.properties           |    4 +
 src/main/java/com/nq/ws/WebsocketRunClient.java     |    4 +-
 src/main/java/com/nq/ws/WebSocketClientExample.java |   89 ++++++++++++++++++++++++++++++++++++++++++++
 src/main/java/com/nq/ws/MXWebsocketRunClient.java   |    3 +
 4 files changed, 96 insertions(+), 4 deletions(-)

diff --git a/src/main/java/com/nq/ws/MXWebsocketRunClient.java b/src/main/java/com/nq/ws/MXWebsocketRunClient.java
index a434ddf..54a96a6 100644
--- a/src/main/java/com/nq/ws/MXWebsocketRunClient.java
+++ b/src/main/java/com/nq/ws/MXWebsocketRunClient.java
@@ -52,7 +52,8 @@
             @Override
             public void run() {
                 if (isOpen()) {
-                    send(("key:"+ eStockType.getStockKey()+":"+eStockType.getContryId()).getBytes());
+                    //send(("key:"+ eStockType.getStockKey()+":"+eStockType.getContryId()).getBytes());
+                    send("heartbeat");
                 }
             }
         }, 0, 3000); // 每3秒发送一次心跳消息
diff --git a/src/main/java/com/nq/ws/WebSocketClientExample.java b/src/main/java/com/nq/ws/WebSocketClientExample.java
new file mode 100644
index 0000000..550a581
--- /dev/null
+++ b/src/main/java/com/nq/ws/WebSocketClientExample.java
@@ -0,0 +1,89 @@
+/*
+package com.nq.ws;
+
+import org.java_websocket.client.WebSocketClient;
+import org.java_websocket.handshake.ServerHandshake;
+import org.json.JSONObject;
+import java.net.URI;
+import java.net.URISyntaxException;
+import java.time.LocalDateTime;
+import java.time.format.DateTimeFormatter;
+import java.util.Timer;
+import java.util.TimerTask;
+
+public class WebSocketClientExample {
+
+    public static void main(String[] args) {
+        try {
+            URI uri = new URI("ws://api-mx-ws.js-stock.top"); // 替换为实际的 WebSocket 地址
+            String key = "key:91kktoeVjh96PB3hnovv:7"; // 替换为实际密钥和国家id
+
+            //URI uri = new URI("ws://api-us-v2-ws.js-stock.top");
+            //String key = "key:SIjHECiI3cIVfHXwsLsL:5";
+            WebSocketClient client = new WebSocketClient(uri) {
+                private Timer heartbeatTimer;
+                @Override
+                public void onOpen(ServerHandshake handshakedata) {
+                    System.out.println("已连接到服务器");
+                    send(key);
+                    // 启动心跳定时器
+                    heartbeatTimer = new Timer();
+                    heartbeatTimer.schedule(new TimerTask() {
+                        @Override
+                        public void run() {
+                            send("heartbeat");
+                        }
+                    }, 0, 3000); // 每3秒发送一次心跳消息
+                }
+                @Override
+                public void onMessage(String message) {
+                    // 获取当前时间并格式化为字符串
+                    LocalDateTime now = LocalDateTime.now();
+                    DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
+                    String timestamp = now.format(formatter);
+                    try {
+                        if (message.contains("身份验证成功") || message.contains("pong") || message.contains("身份验证失败")
+                        || message.contains("ws")) {
+                            System.out.println(timestamp + "" + message);
+                            return;
+                        }
+                        System.out.println("mex" + message);
+                        */
+/*JSONObject json = new JSONObject(message);
+                        String pid = json.optString("pid");
+                        String last = json.optString("last");
+                        // 输出 pid 和 last
+                        System.out.println(timestamp + " pid: " + pid + "价格: " + last);*//*
+
+                    } catch (Exception e) {
+                        System.err.println("处理消息时发生错误: " + message);
+                    }
+                }
+                @Override
+                public void onClose(int code, String reason, boolean remote) {
+                    System.out.println("连接关闭: " + reason);
+                    if (heartbeatTimer != null) {
+                        heartbeatTimer.cancel(); // 取消心跳定时器
+                    }
+                    // 尝试重新连接
+                    try {
+                        System.out.println("尝试重新连接...");
+                        Thread.sleep(10000); // 10秒后尝试重新连接
+                        connect(); // 重新连接
+                    } catch (InterruptedException e) {
+                        e.printStackTrace();
+                    }
+                }
+                @Override
+                public void onError(Exception ex) {
+                    System.err.println("发生错误: " + ex.getMessage());
+                }
+            };
+            client.connect();
+        } catch (URISyntaxException e) {
+            e.printStackTrace();
+        }
+    }
+
+}
+*/
diff --git a/src/main/java/com/nq/ws/WebsocketRunClient.java b/src/main/java/com/nq/ws/WebsocketRunClient.java
index 005191e..9e7549f 100644
--- a/src/main/java/com/nq/ws/WebsocketRunClient.java
+++ b/src/main/java/com/nq/ws/WebsocketRunClient.java
@@ -53,7 +53,8 @@
             @Override
             public void run() {
                 if (isOpen()) {
-                    send(("key:"+ eStockType.getStockKey()+":"+eStockType.getContryId()).getBytes());
+                    //send(("key:"+ eStockType.getStockKey()+":"+eStockType.getContryId()).getBytes());
+                    send("heartbeat");
                 }
 
             }
@@ -87,7 +88,6 @@
             String json = objectMapper.writeValueAsString(stockRealTimeBean);
             sendLoca(json);
             StockRealTimeBean stockDetailBean =  new Gson().fromJson(message, StockRealTimeBean.class);
-            System.out.println(stockDetailBean);
             RedisKeyUtil.setCacheRealTimeStock(EStockType.US,stockDetailBean);
         } catch (JsonProcessingException e) {
             log.error("websocket 美国股票 消息错误:{}", e.getMessage());
diff --git a/src/main/resources/application.properties b/src/main/resources/application.properties
index b35b244..c693a80 100644
--- a/src/main/resources/application.properties
+++ b/src/main/resources/application.properties
@@ -42,12 +42,14 @@
 #股票key的地址
 US_HTTP_API = http://api-us-v2.js-stock.top/
 US_WS_URL = ws://api-us-v2-ws.js-stock.top
-US_KEY = AKoSBQXFrcs02dgcRKde
+US_KEY = SIjHECiI3cIVfHXwsLsL
 
 MX_HTTP_API = http://api-mx.js-stock.top/
 MX_WS_URL = ws://api-mx-ws.js-stock.top
+#MX_KEY = GafNjen0zv8MfJd0OzJF
 MX_KEY = 91kktoeVjh96PB3hnovv
 
+
 #默认首页显示指数code
 us_home_indices_code=15882,15881,16571
 mx_home_indices_code=535610327,535610374,535610333

--
Gitblit v1.9.3