feat:项目打包成Electron

This commit is contained in:
2026-06-14 18:10:58 +08:00
parent 7a0443ebcc
commit 8970b63800
20 changed files with 3895 additions and 11 deletions

View File

@@ -6,10 +6,40 @@
import type { ClientMessage, ServerMessage, WsMessage } from "../types";
// WebSocket 地址:优先使用环境变量,否则基于当前页面地址自动推导
const WS_URL =
import.meta.env.VITE_WS_URL ||
`${window.location.protocol === "https:" ? "wss:" : "ws:"}//${window.location.host}/ws`;
// Electron 类型声明(通过 preload 脚本注入)
interface ElectronAPI {
getBackendUrl: () => string;
isElectron: boolean;
}
declare global {
interface Window {
electronAPI?: ElectronAPI;
}
}
// WebSocket 地址优先级:
// 1. Electron preload 注入的地址(桌面端)
// 2. Vite 环境变量
// 3. 根据当前页面地址自动推导(浏览器端)
function getWsUrl(): string {
// Electron 环境:使用 preload 注入的地址
if (window.electronAPI?.isElectron) {
return window.electronAPI.getBackendUrl();
}
// Vite 环境变量
if (import.meta.env.VITE_WS_URL) {
return import.meta.env.VITE_WS_URL;
}
// 浏览器端:根据当前页面地址推导
const protocol = window.location.protocol === "https:" ? "wss:" : "ws:";
const host = window.location.host || "localhost:8080";
return `${protocol}//${host}/ws`;
}
const WS_URL = getWsUrl();
const PING_INTERVAL = 30_000; // 30 秒心跳
const MAX_RECONNECT_DELAY = 30_000; // 最大重连延迟 30 秒