From 31fead7d32523779e1fc999955727f6dbc7ac782 Mon Sep 17 00:00:00 2001 From: thiszhc <2029364173@qq.com> Date: Fri, 20 Oct 2023 20:04:18 +0800 Subject: [PATCH] =?UTF-8?q?=E5=AE=9E=E7=8E=B0websocket=E4=BB=A3=E7=90=86?= =?UTF-8?q?=EF=BC=8C=E5=88=9D=E5=A7=8B=E5=8C=96=E4=B8=8D=E5=8F=AF=E6=94=BE?= =?UTF-8?q?nav=E5=AF=BC=E8=88=AA=E6=A0=8F=EF=BC=8C=E4=BC=9A=E8=BF=9E?= =?UTF-8?q?=E6=8E=A5=E5=A4=B1=E8=B4=A5=EF=BC=8C=E6=89=80=E4=BB=A5=E5=8F=AA?= =?UTF-8?q?=E8=83=BD=E6=94=BE=E9=A6=96=E9=A1=B5?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .env.development | 2 +- src/layout/components/Navbar.vue | 6 +++--- src/views/index.vue | 2 +- src/views/tool/webSocket/index.vue | 2 +- vite.config.ts | 1 + 5 files changed, 7 insertions(+), 6 deletions(-) diff --git a/.env.development b/.env.development index 5bb03fd..e160334 100644 --- a/.env.development +++ b/.env.development @@ -25,4 +25,4 @@ VITE_APP_RSA_PUBLIC_KEY = 'MFwwDQYJKoZIhvcNAQEBBQADSwAwSAJBAKoR8mX0rGKLqzcWmOzbf VITE_APP_CLIENT_ID = 'e5cd7e4891bf95d1d19206ce24a7b32e' #websocket地址 -VITE_APP_WEBSOCKET_URL = 'ws://127.0.0.1:8098/resource/websocket' \ No newline at end of file +VITE_APP_WEBSOCKET_URL = '/resource/websocket' \ No newline at end of file diff --git a/src/layout/components/Navbar.vue b/src/layout/components/Navbar.vue index eafaaff..013eb1c 100644 --- a/src/layout/components/Navbar.vue +++ b/src/layout/components/Navbar.vue @@ -98,14 +98,13 @@ import { ComponentInternalInstance } from "vue"; import { TenantVO } from "@/api/types"; import notice from './notice/index.vue'; import useNoticeStore from '@/store/modules/notice'; -import { getToken } from '@/utils/auth'; const appStore = useAppStore(); const userStore = useUserStore(); const settingsStore = useSettingsStore(); const noticeStore = storeToRefs(useNoticeStore()); const newNotice = ref(0); -let websocket = new WebSocket(import.meta.env.VITE_APP_WEBSOCKET_URL + '?Authorization=Bearer ' + getToken() + '&clientid=' + import.meta.env.VITE_APP_CLIENT_ID); + const { proxy } = getCurrentInstance() as ComponentInternalInstance; const userId = ref(userStore.userId); @@ -181,7 +180,8 @@ const handleCommand = (command: string) => { commandMap[command](); } } -//用深度监听 + +//用深度监听 消息 watch(() => noticeStore.state.value.notices, (newVal, oldVal) => { newNotice.value = newVal.length; }, { deep: true }); diff --git a/src/views/index.vue b/src/views/index.vue index e740ac6..4896ef1 100644 --- a/src/views/index.vue +++ b/src/views/index.vue @@ -99,7 +99,7 @@ import { initWebSocket } from '@/utils/websocket'; onMounted(() => { -initWebSocket(import.meta.env.VITE_APP_WEBSOCKET_URL); + initWebSocket("ws://"+window.location.host+import.meta.env.VITE_APP_BASE_API+import.meta.env.VITE_APP_WEBSOCKET_URL); }); const goTarget = (url:string) => { diff --git a/src/views/tool/webSocket/index.vue b/src/views/tool/webSocket/index.vue index 4452bb0..60d9a98 100644 --- a/src/views/tool/webSocket/index.vue +++ b/src/views/tool/webSocket/index.vue @@ -38,7 +38,7 @@ interface Conf { } const conf = reactive({ - url: import.meta.env.VITE_APP_WEBSOCKET_URL, // 连接地址 + url: "ws://localhost"+import.meta.env.VITE_APP_BASE_API+import.meta.env.VITE_APP_WEBSOCKET_URL, // 连接地址 send: '123', // 发送信息 message: [], // 消息列表 }); diff --git a/vite.config.ts b/vite.config.ts index cadaf62..788aace 100644 --- a/vite.config.ts +++ b/vite.config.ts @@ -28,6 +28,7 @@ export default defineConfig(({ mode, command }: ConfigEnv): UserConfig => { [env.VITE_APP_BASE_API]: { target: 'http://localhost:8080', changeOrigin: true, + ws: true, rewrite: (path) => path.replace(new RegExp('^' + env.VITE_APP_BASE_API), '') } }