From dde093d321073412ee9f5811f712806da56c148c Mon Sep 17 00:00:00 2001 From: "Wesley F. Young" Date: Mon, 12 Aug 2024 12:29:40 +0800 Subject: [PATCH] fix: hot reload --- src/common/utils/ConfigBase.ts | 5 +++-- src/onebot/index.ts | 24 +++++++++++++----------- 2 files changed, 16 insertions(+), 13 deletions(-) diff --git a/src/common/utils/ConfigBase.ts b/src/common/utils/ConfigBase.ts index 5bb86fbc..ec2b2ef2 100644 --- a/src/common/utils/ConfigBase.ts +++ b/src/common/utils/ConfigBase.ts @@ -53,12 +53,13 @@ export abstract class ConfigBase { } - save(configData: T = this.configData as T) { + save(newConfigData: T = this.configData as T) { const logger = this.coreContext.context.logger; const selfInfo = this.coreContext.selfInfo; + this.configData = newConfigData; const configPath = this.getConfigPath(selfInfo.uin); try { - fs.writeFileSync(configPath, JSON.stringify(configData, this.getKeys(), 2)); + fs.writeFileSync(configPath, JSON.stringify(newConfigData, this.getKeys(), 2)); } catch (e: any) { logger.logError(`保存配置文件 ${configPath} 时发生错误:`, e.message); } diff --git a/src/onebot/index.ts b/src/onebot/index.ts index 7df2f99b..307f0abd 100644 --- a/src/onebot/index.ts +++ b/src/onebot/index.ts @@ -116,6 +116,7 @@ export class NapCatOneBot11Adapter { await WebUiDataRuntime.setOnOB11ConfigChanged(async (newConfig: OB11Config) => { const prev = this.configLoader.configData; this.configLoader.save(newConfig); + this.context.logger.log(`OneBot11 配置更改:${JSON.stringify(prev)} -> ${JSON.stringify(newConfig)}`) await this.reloadNetwork(prev, newConfig); }); } @@ -126,7 +127,7 @@ export class NapCatOneBot11Adapter { HTTP上报服务 ${now.http.enablePost ? '已启动' : '未启动'}, 上报地址: ${now.http.postUrls} WebSocket服务 ${now.ws.enable ? '已启动' : '未启动'}, ${now.ws.host}:${now.ws.port} WebSocket反向服务 ${now.reverseWs.enable ? '已启动' : '未启动'}, 反向地址: ${now.reverseWs.urls}`; - this.context.logger.log(`[Notice] [OneBot11] 热重载完成 ${serviceInfo}`); + this.context.logger.log(`[Notice] [OneBot11] 热重载 ${serviceInfo}`); // check difference in passive http (Http) if (prev.http.enable !== now.http.enable) { @@ -153,14 +154,14 @@ export class NapCatOneBot11Adapter { } else { if (now.http.enablePost) { const { added, removed } = this.findDifference(prev.http.postUrls, now.http.postUrls); - added.forEach(url => { - this.networkManager.registerAdapterAndOpen(new OB11ActiveHttpAdapter( - url, now.token, this.core - )); - }); await this.networkManager.closeAdapterByPredicate( adapter => adapter instanceof OB11ActiveHttpAdapter && removed.includes(adapter.url), ); + for (const url of added) { + await this.networkManager.registerAdapterAndOpen(new OB11ActiveHttpAdapter( + url, now.token, this.core + )); + } } } @@ -193,14 +194,15 @@ export class NapCatOneBot11Adapter { } else { if (now.reverseWs.enable) { const { added, removed } = this.findDifference(prev.reverseWs.urls, now.reverseWs.urls); - added.forEach(url => { - this.networkManager.registerAdapterAndOpen(new OB11ActiveWebSocketAdapter( - url, 5000, now.heartInterval, now.token, this.core, this.actions - )); - }); + console.log('rev ws', added, removed); await this.networkManager.closeAdapterByPredicate( adapter => adapter instanceof OB11ActiveWebSocketAdapter && removed.includes(adapter.url), ); + for (const url of added) { + await this.networkManager.registerAdapterAndOpen(new OB11ActiveWebSocketAdapter( + url, 5000, now.heartInterval, now.token, this.core, this.actions + )); + } } } }