mirror of
https://github.com/NapNeko/NapCatQQ.git
synced 2024-11-16 13:01:14 +00:00
chore: active-websocket
This commit is contained in:
parent
73a8fcd35b
commit
e12c0b5536
@ -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,
|
||||
};
|
||||
}
|
||||
|
@ -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);
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user