Просмотр исходного кода

feat(server): 实现服务器跨窗口启动状态共享和OTA下载缓存优化

- 使用 localStorage 跨窗口共享服务器启动状态避免重复启动
- 添加 isServerStarted 和 markServerStarted 函数管理启动标记
- 在 generateServer 中检查其他窗口是否已启动服务器
- 为 OTA 更新 URL 添加时间戳参数防止缓存问题
- 优化 IPC 通信中的版本更新请求发送逻辑
panqiuyao 1 день назад
Родитель
Сommit
a0a08815fd
2 измененных файлов с 31 добавлено и 8 удалено
  1. 28 5
      frontend/src/utils/generateServer.ts
  2. 3 3
      frontend/src/views/OTA/index.vue

+ 28 - 5
frontend/src/utils/generateServer.ts

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

+ 3 - 3
frontend/src/views/OTA/index.vue

@@ -93,10 +93,12 @@ const clientStore = client();
 const socketStore = socket()
 // 下载特定版本
 const downloadSpecificVersion = (url) => {
+  // 添加时间戳避免缓存
+  const urlWithTimestamp = url + (url.includes('?') ? '&' : '?') + '_t=' + new Date().getTime();
 
   clientStore.ipc.removeAllListeners('app.updater');
   clientStore.ipc.removeAllListeners(icpList.ota.updateVersion);
-  clientStore.ipc.send(icpList.ota.updateVersion,url);
+  clientStore.ipc.send(icpList.ota.updateVersion, urlWithTimestamp);
   clientStore.ipc.on(icpList.ota.updateVersion, async (event, result) => {
     console.log('==============================')
     console.log('checkUpdate')
@@ -126,8 +128,6 @@ const downloadSpecificVersion = (url) => {
 
   })
 
-
-
 };
 
 // 处理分页变化