From 95e138158db3e61e0be67691a4142f20c561634c Mon Sep 17 00:00:00 2001
From: admin <344137771@qq.com>
Date: Tue, 13 Jan 2026 18:50:38 +0800
Subject: [PATCH] 1
---
src/components/stock-list.vue | 40 +++++++++++++++++++++++++++++++++++++++-
1 files changed, 39 insertions(+), 1 deletions(-)
diff --git a/src/components/stock-list.vue b/src/components/stock-list.vue
index a2b8ad3..da8d814 100644
--- a/src/components/stock-list.vue
+++ b/src/components/stock-list.vue
@@ -66,6 +66,7 @@
import nPagination from "@/components/nPagination.vue";
import * as api from "@/axios/api";
import { Toast } from "vant";
+import { WhrWebSocket } from "@/utils/WhrWebSocket";
export default {
name: "stock_list",
components: {
@@ -100,9 +101,16 @@
watch: {
propOption: {
handler(val) {
+ // 根据当前股票类型连接对应的ws
+ if (val.stockType == "US")
+ this.initWebSocket("wss://rbws.shengliankeji.top/websocket-server");
+ else this.initWebSocket("wss://wstt.hebingx.com/websocket-server");
+
this.pageNum = 1;
this.getStockList();
- }
+ },
+ deep: true,
+ immediate: true
},
pageNum: {
handler(val) {
@@ -113,6 +121,7 @@
},
mounted() {
this.getStockList();
+ // this.initWebSocket();
},
methods: {
// 获取数据
@@ -161,6 +170,35 @@
} else {
Toast.fail(data.msg);
}
+ },
+ // 连接ws实时监控变动
+ initWebSocket(url) {
+ console.log("initWebSocket");
+ if (this.Trade) {
+ this.Trade.close();
+ }
+ this.Trade = new WhrWebSocket({
+ path: url,
+ onmessage: this.getTradeMessage
+ });
+
+ this.Trade.init();
+ },
+ getTradeMessage({ data }) {
+ let result = JSON.parse(data);
+ let pid = result.pid;
+ let userToUpdate = this.stockList.find(item => item.code == pid);
+ if (userToUpdate) {
+ // 更新对象数据
+ userToUpdate.nowPrice = result.last;
+ userToUpdate.hcrateP = result.pcp;
+ }
+ }
+ },
+ beforeDestroy() {
+ if (this.Trade) {
+ this.Trade.close();
+ console.log("WebSocket disconnected");
}
}
};
--
Gitblit v1.9.3