feat(electron): adapt renderer API and WebSocket layer for dynamic backend URL

This commit is contained in:
DrSmoothl
2026-03-03 00:54:14 +08:00
parent b5cc361ce9
commit fc394f4412
6 changed files with 55 additions and 27 deletions

View File

@@ -7,6 +7,8 @@ import { checkAuthStatus } from './fetch-with-auth'
import { getSetting } from './settings-manager'
import { createReconnectingWebSocket } from './ws-utils'
import { getWsBaseUrl } from '@/lib/api-base'
export interface LogEntry {
id: string
timestamp: string
@@ -54,18 +56,9 @@ class LogWebSocketManager {
/**
* 获取 WebSocket URL不含 token 参数)
*/
private getWebSocketUrl(): string {
let baseUrl: string
if (import.meta.env.DEV) {
// 开发模式:连接到 WebUI 后端服务器
baseUrl = 'ws://127.0.0.1:8001/ws/logs'
} else {
// 生产模式:使用当前页面的 host
const protocol = window.location.protocol === 'https:' ? 'wss:' : 'ws:'
const host = window.location.host
baseUrl = `${protocol}//${host}/ws/logs`
}
return baseUrl
private async getWebSocketUrl(): Promise<string> {
const wsBase = await getWsBaseUrl()
return `${wsBase}/ws/logs`
}
/**
@@ -85,7 +78,7 @@ class LogWebSocketManager {
return
}
const wsUrl = this.getWebSocketUrl()
const wsUrl = await this.getWebSocketUrl()
// 使用 ws-utils 创建 WebSocket
this.wsControl = createReconnectingWebSocket(wsUrl, {