From 6980a9f3fc9ce1575f515cda3ae50896633229f7 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E6=89=8B=E7=93=9C=E4=B8=80=E5=8D=81=E9=9B=AA?= Date: Mon, 20 May 2024 16:56:34 +0800 Subject: [PATCH] refactor: webui config --- src/common/server/http.ts | 5 ++--- src/onebot11/main.ts | 41 ++++++++++++++++++++++++------------- src/onebot11/server/http.ts | 13 +++++++----- 3 files changed, 37 insertions(+), 22 deletions(-) diff --git a/src/common/server/http.ts b/src/common/server/http.ts index 34ae7d48..72dea6ae 100644 --- a/src/common/server/http.ts +++ b/src/common/server/http.ts @@ -58,9 +58,6 @@ export abstract class HttpServerBase { this.expressAPP.get('/', (req: Request, res: Response) => { res.send(`${this.name}已启动`); }); - this.expressAPP.on('error', (err) => { - logError('HTTP服务启动失败', err.toString()); - }); this.listen(port, host); } catch (e: any) { logError('HTTP服务启动失败', e.toString()); @@ -117,6 +114,8 @@ export abstract class HttpServerBase { this.server = this.expressAPP.listen(port, host, () => { const info = `${this.name} started ${host}:${port}`; log(info); + }).on('error', (err) => { + logError('HTTP服务启动失败', err.toString()); }); } catch (e: any) { logError('HTTP服务启动失败, 请检查监听的ip地址和端口', e.stack.toString()); diff --git a/src/onebot11/main.ts b/src/onebot11/main.ts index afd811d0..6c2ef813 100644 --- a/src/onebot11/main.ts +++ b/src/onebot11/main.ts @@ -259,22 +259,44 @@ export class NapCatOnebot11 { // throw new Error('Invalid configuration object'); // } + // 先设置 + ob11Config.save(NewOb11); + const isHttpChanged = !isEqual(NewOb11.http.port, ob11Config.http.port) && NewOb11.http.enable; + const isHttpEnableChanged = !isEqual(NewOb11.http.enable, ob11Config.http.enable); + + const isHttpPostChanged = !isEqual(NewOb11.http.postUrls, ob11Config.http.postUrls); + const isEnanleHttpPostChanged = !isEqual(NewOb11.http.enablePost, ob11Config.http.enablePost); + const isWsChanged = !isEqual(NewOb11.ws.port, ob11Config.ws.port); const isEnableWsChanged = !isEqual(NewOb11.ws.enable, ob11Config.ws.enable); + const isEnableWsReverseChanged = !isEqual(NewOb11.reverseWs.enable, ob11Config.reverseWs.enable); const isWsReverseUrlsChanged = !isEqual(NewOb11.reverseWs.urls, ob11Config.reverseWs.urls); + const isEnableHeartBeatChanged = !isEqual(NewOb11.heartInterval, ob11Config.heartInterval); + // http重启逻辑 if (isHttpChanged) { ob11HTTPServer.restart(NewOb11.http.port, NewOb11.http.host); } - if (!NewOb11.http.enable) { - ob11HTTPServer.stop(); - } else { - ob11HTTPServer.start(NewOb11.http.port, NewOb11.http.host); + if (isHttpEnableChanged) { + if (NewOb11.http.enable) { + ob11HTTPServer.start(NewOb11.http.port, NewOb11.http.host); + } else { + ob11HTTPServer.stop(); + } } + // http post重启逻辑 + // if(isHttpPostChanged){ + // logDebug('http post urls changed, restart http server'); + // } + // if(isEnanleHttpPostChanged){ + // logDebug('http post enable changed, restart http server'); + // } + + // ws重启逻辑 if (isWsChanged) { ob11WebsocketServer.restart(NewOb11.ws.port); } @@ -287,6 +309,7 @@ export class NapCatOnebot11 { } } + // 反向ws重启逻辑 if (isEnableWsReverseChanged) { if (NewOb11.reverseWs.enable) { ob11ReverseWebsockets.start(); @@ -294,16 +317,6 @@ export class NapCatOnebot11 { ob11ReverseWebsockets.stop(); } } - if (NewOb11.reverseWs.enable && isWsReverseUrlsChanged) { - logDebug('反向ws地址有变化, 重启反向ws服务'); - ob11ReverseWebsockets.restart(); - } - if (NewOb11.http.enableHeart) { - httpHeart.start(); - } else if (!NewOb11.http.enableHeart) { - httpHeart.stop(); - } - ob11Config.save(NewOb11); } catch (e) { logError('热重载配置失败', e); } diff --git a/src/onebot11/server/http.ts b/src/onebot11/server/http.ts index 28bf89a5..ea3b6fdd 100644 --- a/src/onebot11/server/http.ts +++ b/src/onebot11/server/http.ts @@ -35,10 +35,13 @@ setTimeout(() => { }, 0); -class HTTPHeart{ +class HTTPHeart { intervalId: NodeJS.Timeout | null = null; - start(){ - const { heartInterval, } = ob11Config; + start(NewHeartInterval: number | undefined = undefined) { + let { heartInterval } = ob11Config; + if (NewHeartInterval && !Number.isNaN(NewHeartInterval)) { + heartInterval = NewHeartInterval; + } if (this.intervalId) { clearInterval(this.intervalId); } @@ -48,8 +51,8 @@ class HTTPHeart{ }, heartInterval); } - stop(){ - if (this.intervalId){ + stop() { + if (this.intervalId) { clearInterval(this.intervalId); } }