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

@ -28,7 +28,7 @@ json 配置内容参数解释:
```json5
{
// 是否启用http服务如果启用可以通过http接口发送消息
// 是否启用http服务, true为启动false为禁用如果启用可以通过http接口发送消息
"enableHttp": false,
// http服务端口
"httpPort": 3000,
@ -44,6 +44,8 @@ json 配置内容参数解释:
"enableHttpPost": false,
// http上报地址, 如["http://127.0.0.1:8080/onebot/v11/http"]
"httpPostUrls": [],
// 是否启用http心跳
"enableHttpHeart": false,
// http上报密钥可为空
"httpSecret": "",
// 消息上报格式array为消息组string为cq码字符串

View File

@ -9,6 +9,7 @@ export interface OB11Config {
wsPort: number;
wsReverseUrls: string[];
enableHttp: boolean;
enableHttpHeart: boolean;
enableHttpPost: boolean;
enableWs: boolean;
enableWsReverse: boolean;
@ -35,6 +36,7 @@ class Config implements OB11Config {
wsReverseUrls: string[] = [];
enableHttp = false;
enableHttpPost = false;
enableHttpHeart = false;
enableWs = false;
enableWsReverse = false;
messagePostFormat: 'array' | 'string' = 'array';

View File

@ -12,7 +12,7 @@ import {
RawMessage
} from '@/core/qqnt/entities';
import { ob11Config } from '@/onebot11/config';
import { ob11HTTPServer } from '@/onebot11/server/http';
import { httpHeart, ob11HTTPServer } from '@/onebot11/server/http';
import { ob11WebsocketServer } from '@/onebot11/server/ws/WebsocketServer';
import { ob11ReverseWebsockets } from '@/onebot11/server/ws/ReverseWebsocket';
import { friendRequests, getFriend, getGroup, getGroupMember, groupNotifies, selfInfo } from '@/common/data';
@ -49,6 +49,10 @@ export class NapCatOnebot11 {
if (ob11Config.enableWsReverse) {
ob11ReverseWebsockets.start();
}
if (ob11Config.enableHttpHeart){
// 启动http心跳
httpHeart.start();
}
// MsgListener
const msgListener = new MsgListener();
msgListener.onRecvSysMsg = (protobuf: number[]) => {

View File

@ -7,6 +7,7 @@
"wsReverseUrls": [],
"enableHttpPost": false,
"httpPostUrls": [],
"enableHttpHeart": false,
"httpSecret": "",
"messagePostFormat": "array",
"reportSelfMessage": false,

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);
}
}