|
|
@@ -11,12 +11,33 @@ import pinia from "@/stores/index";
|
|
|
// @ts-ignore - Node.js require 在Electron环境中可用
|
|
|
type NodeRequire = typeof require
|
|
|
|
|
|
-let started = false
|
|
|
+// 使用 localStorage 来跨窗口共享服务器启动状态
|
|
|
+const SERVER_STARTED_KEY = 'generate_server_started'
|
|
|
+
|
|
|
+function isServerStarted(): boolean {
|
|
|
+ try {
|
|
|
+ return localStorage.getItem(SERVER_STARTED_KEY) === 'true'
|
|
|
+ } catch {
|
|
|
+ return false
|
|
|
+ }
|
|
|
+}
|
|
|
+
|
|
|
+function markServerStarted(): void {
|
|
|
+ try {
|
|
|
+ localStorage.setItem(SERVER_STARTED_KEY, 'true')
|
|
|
+ } catch {
|
|
|
+ // localStorage 不可用时的降级处理
|
|
|
+ }
|
|
|
+}
|
|
|
|
|
|
export async function startGenerateServer( ) {
|
|
|
setTimeout(() => {
|
|
|
|
|
|
- if (started) return
|
|
|
+ // 检查服务器是否已在其他窗口启动
|
|
|
+ if (isServerStarted()) {
|
|
|
+ console.log('[generateServer] server already started in another window')
|
|
|
+ return
|
|
|
+ }
|
|
|
|
|
|
// 仅在具有 Node 能力(Electron 渲染进程)时启动
|
|
|
const nodeRequire: NodeRequire | undefined = (window as any)?.require
|
|
|
@@ -25,9 +46,11 @@ export async function startGenerateServer( ) {
|
|
|
}
|
|
|
|
|
|
const appConfig = pinia.state.value.config?.appConfig;
|
|
|
- const PORT = appConfig.PORT || 3001
|
|
|
- const http = nodeRequire('http')
|
|
|
- started = true
|
|
|
+ const PORT = appConfig?.PORT || 3001
|
|
|
+ const http = nodeRequire('http')
|
|
|
+
|
|
|
+ // 标记服务器已启动(使用 localStorage 跨窗口共享)
|
|
|
+ markServerStarted()
|
|
|
|
|
|
http
|
|
|
.createServer(async (req: any, res: any) => {
|