chore: active-websocket

This commit is contained in:
手瓜一十雪 2024-08-11 00:19:28 +08:00
parent 73a8fcd35b
commit e12c0b5536
2 changed files with 12 additions and 5 deletions

View File

@ -2,7 +2,7 @@ import { OB11BaseMetaEvent } from './OB11BaseMetaEvent';
import { NapCatCore } from '@/core';
interface HeartbeatStatus {
online: boolean | null,
online: boolean | undefined,
good: boolean
}
@ -11,11 +11,11 @@ export class OB11HeartbeatEvent extends OB11BaseMetaEvent {
status: HeartbeatStatus;
interval: number;
public constructor(core: NapCatCore, interval: number, isOnline: boolean | null, isGood: boolean) {
public constructor(core: NapCatCore, interval: number, isOnline: boolean | undefined, isGood: boolean) {
super(core);
this.interval = interval;
this.status = {
online: isOnline,
online: isOnline && true,
good: isGood,
};
}

View File

@ -2,6 +2,9 @@ import { IOB11NetworkAdapter, OB11EmitEventContent } from '@/onebot/network/inde
import { WebSocket as NodeWebSocket } from 'ws';
import BaseAction from '@/onebot/action/BaseAction';
import { sleep } from '@/common/utils/helper';
import { OB11HeartbeatEvent } from '../event/meta/OB11HeartbeatEvent';
import { NapCatCore } from '@/core';
import { NapCatOneBot11Adapter } from '../main';
export class OB11ActiveWebSocketAdapter implements IOB11NetworkAdapter {
url: string;
@ -11,11 +14,15 @@ export class OB11ActiveWebSocketAdapter implements IOB11NetworkAdapter {
private actionMap: Map<string, BaseAction<any, any>> = new Map();
heartbeatInterval: number;
private heartbeatTimer: NodeJS.Timeout | null = null;
onebotContext: NapCatOneBot11Adapter;
coreContext: NapCatCore;
constructor(url: string, reconnectIntervalInMillis: number, heartbeatInterval: number) {
constructor(url: string, reconnectIntervalInMillis: number, heartbeatInterval: number, coreContext: NapCatCore, onebotContext: NapCatOneBot11Adapter) {
this.url = url;
this.heartbeatInterval = heartbeatInterval;
this.reconnectIntervalInMillis = reconnectIntervalInMillis;
this.coreContext = coreContext;
this.onebotContext = onebotContext;
}
registerHeartBeat() {
@ -23,7 +30,7 @@ export class OB11ActiveWebSocketAdapter implements IOB11NetworkAdapter {
if (this.connection) {
this.heartbeatTimer = setInterval(() => {
if (this.connection && this.connection.readyState === NodeWebSocket.OPEN) {
this.connection.ping();
this.connection.ping(JSON.stringify(new OB11HeartbeatEvent(this.coreContext, this.heartbeatInterval, this.coreContext.selfInfo.online, true)));
}
}, this.heartbeatInterval);
}