From 34206bf9e9cb18de6eeea0b33df5aeaf81d43fd9 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E9=83=9D=E5=85=88=E7=91=9E?= <1490493387@qq.com> Date: Mon, 4 Sep 2023 21:29:24 +0800 Subject: [PATCH] =?UTF-8?q?feat:=20:sparkles:=20websocket=E7=A4=BA?= =?UTF-8?q?=E4=BE=8B=E4=B8=B4=E6=97=B6=E6=8F=90=E4=BA=A4?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Former-commit-id: 3a71f6f232c69bc98bc9728161fe49e3e94e9f58 --- src/layout/components/Settings/index.vue | 20 ++++++- src/views/demo/websocket.vue | 75 +++++++----------------- 2 files changed, 40 insertions(+), 55 deletions(-) diff --git a/src/layout/components/Settings/index.vue b/src/layout/components/Settings/index.vue index f66ac1d1..d370b269 100644 --- a/src/layout/components/Settings/index.vue +++ b/src/layout/components/Settings/index.vue @@ -5,14 +5,32 @@ import { useAppStore } from "@/store/modules/app"; import IconEpSunny from "~icons/ep/sunny"; import IconEpMoon from "~icons/ep/moon"; import { useRoute } from "vue-router"; - +import defaultSettings from "@/settings"; /** * 暗黑模式 */ const settingsStore = useSettingsStore(); const permissionStore = usePermissionStore(); const appStore = useAppStore(); + const isDark = useDark(); +console.log("isDark1", isDark.value); + +watch( + () => defaultSettings.theme, + (newVal: string) => { + if ( + (newVal == "dark" && isDark.value == false) || + (newVal == "light" && isDark.value == true) + ) { + toggleDark(); + console.log("isDark2", isDark.value); + } + } +); + +computed(() => {}); + const toggleDark = () => useToggle(isDark); function findOutermostParent(tree: any[], findName: string) { diff --git a/src/views/demo/websocket.vue b/src/views/demo/websocket.vue index ce54afc2..ffbc4c7a 100644 --- a/src/views/demo/websocket.vue +++ b/src/views/demo/websocket.vue @@ -4,7 +4,9 @@ import { sendToAll, sendToUser } from "@/api/websocket"; // 点对点消息列 import { useUserStore } from "@/store/modules/user"; -import { useWebSocket } from "@vueuse/core"; +import SockJS from "sockjs-client"; // 报错 global is not defined 换成下面的引入 +// import SockJS from "sockjs-client/dist/sockjs.min.js"; +import Stomp from "stompjs"; const inputVal = ref("初始内容"); @@ -13,58 +15,6 @@ const p2pMsgs = ref(["接收到一条点对线消息"]); const userId = useUserStore().userId; -const { data, status, close, send, open } = useWebSocket( - "ws://localhost:8989/ws", - { - onConnected(ws) { - console.log("订阅主题"); - // 连接建立后发送订阅消息 - ws.send(JSON.stringify({ type: "subscribe", topic: "/topic/all" })); - }, - onMessage(ws, event) { - // 获取接收到的消息内容 - const message = event.data; - - // 处理消息内容 - console.log("Received message:", message); - }, - } -); - -// 监听 WebSocket 连接状态变化 -watch(status, (newStatus) => { - if (newStatus === "OPEN") { - // 连接已打开,订阅主题 - console.log(" 连接已打开,订阅主题"); - const subscribeMessage = { - type: "subscribe", - channel: "/topic/all", - }; - send(JSON.stringify(subscribeMessage)); - } else if (newStatus === "CLOSED") { - // 连接已关闭,执行相应的清理操作 - // ... - } -}); - -// 监听 WebSocket 接收到的消息 -watch(data, (newData) => { - console.log("Received data:", newData); - - // 解析消息体 - const message = JSON.parse(newData); - - // 判断消息主题并处理 - if (message.topic === "topic1") { - // 处理来自 topic1 的消息 - console.log("Received message from topic1:", message); - } else if (message.topic === "topic2") { - // 处理来自 topic2 的消息 - console.log("Received message from topic2:", message); - } - // 可以根据需要添加更多的判断逻辑来处理其他主题的消息 -}); - function handleSendToAll() { sendToAll(inputVal.value); } @@ -72,7 +22,24 @@ function handleSendToAll() { function handleSendToUser() { sendToUser(userId, inputVal.value); } -onMounted(() => {}); + +let stompClient: Stomp.Client; + +function initWebSocket() { + stompClient = Stomp.overWS("ws://localhost:8989/ws"); + + stompClient.connect({}, () => { + console.log("连接成功"); + + stompClient.subscribe("/topic/all", (res) => { + console.log("订阅响应"); + }); + }); +} + +onMounted(() => { + initWebSocket(); +});