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(); });