From bba48e448f578a67de7f5e33eb56ab05e5a540da Mon Sep 17 00:00:00 2001 From: ray <1490493387@qq.com> Date: Sun, 25 Aug 2024 08:42:03 +0800 Subject: [PATCH] =?UTF-8?q?feat:=20:sparkles:=20=E5=AE=9E=E6=97=B6?= =?UTF-8?q?=E5=9C=A8=E7=BA=BF=E7=94=A8=E6=88=B7=E7=BB=9F=E8=AE=A1?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/views/dashboard/index.vue | 70 ++++++++++++++++++++++++++--------- 1 file changed, 52 insertions(+), 18 deletions(-) diff --git a/src/views/dashboard/index.vue b/src/views/dashboard/index.vue index 373b9906..142c3a35 100644 --- a/src/views/dashboard/index.vue +++ b/src/views/dashboard/index.vue @@ -45,22 +45,22 @@
- 1 + 1
- 总用户数 - 5 + 总用户数 + 5
@@ -102,9 +102,9 @@ @@ -151,12 +151,13 @@ @@ -192,12 +193,15 @@ defineOptions({ inheritAttrs: false, }); +import { Client } from "@stomp/stompjs"; + import { useUserStore } from "@/store/modules/user"; import { NoticeTypeEnum, getNoticeLabel } from "@/enums/NoticeTypeEnum"; - +import { TOKEN_KEY } from "@/enums/CacheEnum"; import StatsAPI, { VisitStatsVO } from "@/api/log"; const userStore = useUserStore(); +const socketEndpoint = ref(import.meta.env.VITE_APP_WS_ENDPOINT); const date: Date = new Date(); const greetings = computed(() => { const hours = date.getHours(); @@ -237,6 +241,8 @@ const statisticData = ref([ }, ]); +const onlineUserCount = ref(0); + const visitStatsLoading = ref(true); const visitStatsList = ref(Array(3).fill({})); interface VisitStats { @@ -384,8 +390,36 @@ const getNoticeLevelTag = (type: number) => { } }; +let stompClient: Client; + +function connectWebSocket() { + stompClient = new Client({ + brokerURL: socketEndpoint.value, + connectHeaders: { + Authorization: localStorage.getItem(TOKEN_KEY) || "", + }, + debug: (str) => { + console.log(str); + }, + onConnect: () => { + console.log("连接成功"); + stompClient.subscribe("/topic/onlineUserCount", (message) => { + onlineUserCount.value = JSON.parse(message.body); + }); + }, + onStompError: (frame) => { + console.error("Broker reported error: " + frame.headers["message"]); + console.error("Additional details: " + frame.body); + }, + onDisconnect: () => {}, + }); + + stompClient.activate(); +} + onMounted(() => { loadVisitStatsData(); + connectWebSocket(); });