diff --git a/src/utils/websocket.js b/src/utils/websocket.js new file mode 100644 index 0000000..729a8ad --- /dev/null +++ b/src/utils/websocket.js @@ -0,0 +1,20 @@ +const getWebSocketTarget = () => { + if (import.meta.env.DEV && typeof __APP_TARGET__ !== 'undefined' && __APP_TARGET__) { + return __APP_TARGET__; + } + return window.location.host; +}; + +export const buildWebSocketUrl = (path, params = {}) => { + const wsProtocol = window.location.protocol === 'https:' ? 'wss:' : 'ws:'; + const normalizedPath = path.startsWith('/') ? path : `/${path}`; + const url = new URL(`${wsProtocol}//${getWebSocketTarget()}${normalizedPath}`); + + Object.entries(params).forEach(([key, value]) => { + if (value !== undefined && value !== null && value !== '') { + url.searchParams.set(key, value); + } + }); + + return url.toString(); +}; diff --git a/src/views/InstanceDetail.vue b/src/views/InstanceDetail.vue index e772251..264cd5a 100644 --- a/src/views/InstanceDetail.vue +++ b/src/views/InstanceDetail.vue @@ -306,6 +306,7 @@ import { useRoute, useRouter } from 'vue-router'; import { instanceApi } from '../api/index.js'; import { showNotification, formatDate, getCookie } from '../utils/functions.js'; import { showFSDialog } from '../utils/dialogManager.js'; +import { buildWebSocketUrl } from '../utils/websocket.js'; export default { name: 'InstanceDetail', @@ -452,9 +453,10 @@ export default { const connectLogWebSocket = () => { const token = getCookie('token'); - const wsProtocol = window.location.protocol === 'https:' ? 'wss:' : 'ws:'; - const target = __APP_TARGET__; - const wsUrl = `${wsProtocol}//${target}/api/frpcAct/instanceMgr/logs?instanceID=${instanceID.value}&token=${token}`; + const wsUrl = buildWebSocketUrl('/api/frpcAct/instanceMgr/logs', { + instanceID: instanceID.value, + token + }); logSocket = new WebSocket(wsUrl); diff --git a/src/views/Logs.vue b/src/views/Logs.vue index 2dfe189..2622939 100644 --- a/src/views/Logs.vue +++ b/src/views/Logs.vue @@ -31,6 +31,7 @@