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

View File

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