diff --git a/src/onebot/event/meta/OB11HeartbeatEvent.ts b/src/onebot/event/meta/OB11HeartbeatEvent.ts index a1c43a0e..41d16ba9 100644 --- a/src/onebot/event/meta/OB11HeartbeatEvent.ts +++ b/src/onebot/event/meta/OB11HeartbeatEvent.ts @@ -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, }; } diff --git a/src/onebot/network/active-websocket.ts b/src/onebot/network/active-websocket.ts index 674ef009..a3437a00 100644 --- a/src/onebot/network/active-websocket.ts +++ b/src/onebot/network/active-websocket.ts @@ -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> = 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); }