fix: http.get error handler, separate stop/exit kill logic

This commit is contained in:
zlm committed 2026-06-10 15:08:49 +08:00
1 parent 1e92cc85fe
commit 3436d5bb6a
1 file changed
+10 -6
+10 -6
View File
@@ -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(() => {
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()
}, 1000)
}
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())