From 9ddad348a549154e7b328f9d7dd9517d2abd330c Mon Sep 17 00:00:00 2001
From: PC-20250623MANY\Administrator <344137771@qq.com>
Date: Tue, 30 Sep 2025 09:47:25 +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..2a42cf9 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://usws.yanshiz.com/websocket-server");
+ else this.initWebSocket("wss://ws.acapl.net/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