实现websocket代理,初始化不可放nav导航栏,会连接失败,所以只能放首页

This commit is contained in:
thiszhc 2023-10-20 20:04:18 +08:00
parent 9150b88334
commit 31fead7d32
5 changed files with 7 additions and 6 deletions

View File

@ -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'
VITE_APP_WEBSOCKET_URL = '/resource/websocket'

View File

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

View File

@ -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) => {

View File

@ -38,7 +38,7 @@ interface Conf {
}
const conf = reactive(<Conf>{
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: [], //
});

View File

@ -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), '')
}
}