feat: http heart

This commit is contained in:
linyuchen
2024-04-16 20:58:29 +08:00
parent 01ab40bf4a
commit 66cc7f8a1f
6 changed files with 42 additions and 4 deletions

View File

@@ -3,6 +3,10 @@ import { OB11Response } from '../action/OB11Response';
import { HttpServerBase } from '@/common/server/http';
import { actionHandlers, actionMap } from '../action';
import { ob11Config } from '@/onebot11/config';
import { selfInfo } from '@/common/data';
import { OB11HeartbeatEvent } from '@/onebot11/event/meta/OB11HeartbeatEvent';
import { postOB11Event } from '@/onebot11/server/postOB11Event';
import { napCatCore } from '@/core';
class OB11HTTPServer extends HttpServerBase {
name = 'OneBot V11 server';
@@ -29,3 +33,26 @@ setTimeout(() => {
}
}
}, 0);
class HTTPHeart{
intervalId: NodeJS.Timeout | null = null;
start(){
const { heartInterval, } = ob11Config;
if (this.intervalId) {
clearInterval(this.intervalId);
}
this.intervalId = setInterval(() => {
// ws的心跳是ws自己维护的
postOB11Event(new OB11HeartbeatEvent(!!selfInfo.online, true, heartInterval), false, false);
}, heartInterval);
}
stop(){
if (this.intervalId){
clearInterval(this.intervalId);
}
}
}
export const httpHeart = new HTTPHeart();

View File

@@ -69,7 +69,7 @@ export function postWsEvent(event: PostEventType) {
}
}
export function postOB11Event(msg: PostEventType, reportSelf = false) {
export function postOB11Event(msg: PostEventType, reportSelf = false, postWs= true) {
const config = ob11Config;
// 判断msg是否是event
if (!config.reportSelfMessage && !reportSelf) {
@@ -181,5 +181,7 @@ export function postOB11Event(msg: PostEventType, reportSelf = false) {
});
}
}
postWsEvent(msg);
if (postWs){
postWsEvent(msg);
}
}