feat: http与ws共站支持热重载

This commit is contained in:
挂神 2024-07-24 13:02:37 +08:00
parent f7bdc35ed6
commit 25bca8385d
4 changed files with 62 additions and 37 deletions

View File

@ -70,11 +70,13 @@ export class WebsocketServerBase {
}
stop() {
this.ws && this.ws.close((err) => {
log('ws server close failed!', err);
if (this.ws) {
this.ws.close((err) => {
if (err) log('ws server close failed!', err);
});
this.ws = null;
}
}
restart(port: number) {
this.stop();

View File

@ -91,8 +91,10 @@ export function enableConsoleLog(enable: boolean) {
function formatMsg(msg: any[]) {
let logMsg = '';
for (const msgItem of msg) {
// 判断是否是对象
if (typeof msgItem === 'object') {
if (msgItem instanceof Error) { // 判断是否是错误
logMsg += msgItem.stack + ' ';
continue;
} else if (typeof msgItem === 'object') { // 判断是否是对象
const obj = JSON.parse(JSON.stringify(msgItem, null, 2));
logMsg += JSON.stringify(truncateString(obj)) + ' ';
continue;

View File

@ -70,7 +70,7 @@ const showQRCode = async (url: string, base64: string, buffer: Buffer) => {
});
};
let quickLoginQQ = cmdOptions.qq;
let quickLoginQQ = cmdOptions.qq; // undefine、true、string
const QuickLoginList = await napCatCore.getQuickLoginList();
if (quickLoginQQ == true) {
if (QuickLoginList.LocalLoginInfoList.length > 0) {

View File

@ -46,6 +46,10 @@ export let DeviceList = new Array<LineDevice>();
//peer->cached(boolen)
// const PokeCache = new Map<string, boolean>();
function check_http_ws_equal(conf: any) { // 放在NapCatOnebot11里能被onReady调用却不能被SetConfig调用 不知道为什么 只能放这里了
return isEqual(conf.http.port, conf.ws.port) && isEqual(conf.http.host, conf.ws.host);
}
export class NapCatOnebot11 {
private bootTime: number = Date.now() / 1000; // 秒
@ -73,7 +77,7 @@ export class NapCatOnebot11 {
ob11HTTPServer.start(ob11Config.http.port, ob11Config.http.host);
}
if (ob11Config.ws.enable) {
if (ob11Config.http.port == ob11Config.ws.port && ob11Config.http.host == ob11Config.ws.host && ob11HTTPServer.server) {
if (check_http_ws_equal(ob11Config) && ob11HTTPServer.server) { // ob11HTTPServer.server != null 隐含了 ob11Config.http.enable == true 的条件
ob11WebsocketServer.start(ob11HTTPServer.server);
} else {
ob11WebsocketServer.start(ob11Config.ws.port, ob11Config.ws.host);
@ -383,49 +387,66 @@ export class NapCatOnebot11 {
const OldConfig = JSON.parse(JSON.stringify(ob11Config)); //进行深拷贝
ob11Config.save(NewOb11);//保存新配置
const isHttpChanged = !isEqual(NewOb11.http.port, OldConfig.http.port);
const isHttpEnableChanged = !isEqual(NewOb11.http.enable, OldConfig.http.enable);
const isHttpChanged = !isEqual(NewOb11.http.enable, OldConfig.http.enable) ||
!isEqual(NewOb11.http.host, OldConfig.http.host) ||
!isEqual(NewOb11.http.port, OldConfig.http.port);
// const isHttpPostChanged = !isEqual(NewOb11.http.postUrls, OldConfig.http.postUrls);
// const isEnanleHttpPostChanged = !isEqual(NewOb11.http.enablePost, OldConfig.http.enablePost);
const isWsChanged = !isEqual(NewOb11.ws.port, OldConfig.ws.port);
const isEnableWsChanged = !isEqual(NewOb11.ws.enable, OldConfig.ws.enable);
const isWsChanged = !isEqual(NewOb11.ws.enable, OldConfig.ws.enable) ||
!isEqual(NewOb11.ws.host, OldConfig.ws.host) ||
!isEqual(NewOb11.ws.port, OldConfig.ws.port);
const isEnableWsReverseChanged = !isEqual(NewOb11.reverseWs.enable, OldConfig.reverseWs.enable);
const isWsReverseUrlsChanged = !isEqual(NewOb11.reverseWs.urls, OldConfig.reverseWs.urls);
const isWsReverseChanged = !isEqual(NewOb11.reverseWs.enable, OldConfig.reverseWs.enable) ||
!isEqual(NewOb11.reverseWs.urls, OldConfig.reverseWs.urls);
//const isEnableHeartBeatChanged = !isEqual(NewOb11.heartInterval, OldConfig.heartInterval);
// http重启逻辑
// console.log(isHttpEnableChanged, isHttpChanged, NewOb11.http.enable);
if ((isHttpEnableChanged || isHttpChanged) && NewOb11.http.enable) {
if (OldConfig.http.enable) {
if (check_http_ws_equal(NewOb11) || check_http_ws_equal(OldConfig)) {
// http与ws共站 需要同步重启
if (isHttpChanged || isWsChanged) {
log("http与ws进行热重载")
ob11WebsocketServer.stop();
ob11HTTPServer.stop();
}
if (NewOb11.http.enable) {
ob11HTTPServer.start(NewOb11.http.port, NewOb11.http.host);
} else if (isHttpEnableChanged && !NewOb11.http.enable) {
}
if (NewOb11.ws.enable) {
if (check_http_ws_equal(NewOb11) && ob11HTTPServer.server) {
ob11WebsocketServer.start(ob11HTTPServer.server);
} else {
ob11WebsocketServer.start(NewOb11.ws.port, NewOb11.ws.host);
}
}
}
} else {
// http重启逻辑
if (isHttpChanged) {
log("http进行热重载")
ob11HTTPServer.stop();
if (NewOb11.http.enable) {
ob11HTTPServer.start(NewOb11.http.port, NewOb11.http.host);
}
}
// ws重启逻辑
if ((isEnableWsChanged || isWsChanged) && NewOb11.ws.enable) {
if (OldConfig.ws.enable) {
if (isWsChanged) {
log("ws进行热重载")
ob11WebsocketServer.stop();
}
if (NewOb11.ws.enable) {
ob11WebsocketServer.start(NewOb11.ws.port, NewOb11.ws.host);
} else if (isHttpEnableChanged && !NewOb11.http.enable) {
ob11WebsocketServer.stop();
}
}
}
// 反向ws重启逻辑
if ((isEnableWsReverseChanged || isWsReverseUrlsChanged) && NewOb11.reverseWs.enable) {
if (OldConfig.reverseWs.enable) {
if (isWsReverseChanged) {
log("反向ws进行热重载")
ob11ReverseWebsockets.stop();
}
if (NewOb11.reverseWs.enable) {
ob11ReverseWebsockets.start();
} else if (isHttpEnableChanged && !NewOb11.http.enable) {
ob11ReverseWebsockets.stop();
}
}
} catch (e) {