diff --git a/desktop/src/main.js b/desktop/src/main.js index 4ac7ee8..2e36a1b 100644 --- a/desktop/src/main.js +++ b/desktop/src/main.js @@ -1,5 +1,6 @@ const { app, Tray, Menu, nativeImage } = require('electron') const { spawn, exec } = require('child_process') +const http = require('http') const path = require('path') const PROJECT_DIR = path.resolve(__dirname, '../../project') @@ -18,9 +19,9 @@ function updateMenu() { { label: '打开页面', click: () => exec('start http://localhost:3000') }, { type: 'separator' }, { label: '启动服务', enabled: !isRunning, click: startBackend }, - { label: '停止服务', enabled: isRunning, click: stopBackend }, + { label: '停止服务', enabled: isRunning, click: () => stopBackend() }, { type: 'separator' }, - { label: '退出', click: () => { stopBackend(); app.quit() } }, + { label: '退出', click: () => { killBackend(); app.quit() } }, ])) } @@ -39,13 +40,16 @@ function startBackend() { function stopBackend() { if (!backend) return - try { require('http').get('http://localhost:3000/api/shutdown') } catch {} - setTimeout(() => { - try { backend.kill('SIGTERM') } catch {} - backend = null - isRunning = false - updateMenu() - }, 1000) + http.get('http://localhost:3000/api/shutdown').on('error', () => {}) + setTimeout(killBackend, 1000) +} + +function killBackend() { + if (!backend) return + try { backend.kill('SIGTERM') } catch {} + backend = null + isRunning = false + updateMenu() } app.whenReady().then(() => { @@ -56,4 +60,4 @@ app.whenReady().then(() => { }) app.on('window-all-closed', () => {}) -app.on('before-quit', () => stopBackend()) +app.on('before-quit', () => killBackend())