mirror of
https://github.com/NapNeko/NapCatQQ.git
synced 2024-11-21 09:36:35 +00:00
feat: complete active websocket
This commit is contained in:
parent
3c42cc17c8
commit
a955937e02
@ -1,41 +1,38 @@
|
|||||||
import { IOB11NetworkAdapter, OB11EmitEventContent } from '@/onebot/network/index';
|
import { IOB11NetworkAdapter, OB11EmitEventContent } from '@/onebot/network/index';
|
||||||
import { WebSocket as NodeWebSocket } from 'ws';
|
import { WebSocket, 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 { OB11HeartbeatEvent } from '../event/meta/OB11HeartbeatEvent';
|
||||||
import { NapCatCore } from '@/core';
|
import { NapCatCore } from '@/core';
|
||||||
import { NapCatOneBot11Adapter } from '../main';
|
import { NapCatOneBot11Adapter } from '../main';
|
||||||
|
import { ActionName } from '@/onebot/action/types';
|
||||||
|
import { OB11Response } from '@/onebot/action/OB11Response';
|
||||||
|
import { LogWrapper } from '@/common/utils/log';
|
||||||
|
|
||||||
export class OB11ActiveWebSocketAdapter implements IOB11NetworkAdapter {
|
export class OB11ActiveWebSocketAdapter implements IOB11NetworkAdapter {
|
||||||
url: string;
|
url: string;
|
||||||
reconnectIntervalInMillis: number;
|
reconnectIntervalInMillis: number;
|
||||||
isClosed: boolean = false;
|
isClosed: boolean = false;
|
||||||
|
heartbeatInterval: number;
|
||||||
|
obContext: NapCatOneBot11Adapter;
|
||||||
|
coreContext: NapCatCore;
|
||||||
|
logger: LogWrapper;
|
||||||
private connection: NodeWebSocket | null = null;
|
private connection: NodeWebSocket | null = null;
|
||||||
private actionMap: Map<string, BaseAction<any, any>> = new Map();
|
private actionMap: Map<string, BaseAction<any, any>> = new Map();
|
||||||
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, coreContext: NapCatCore, onebotContext: NapCatOneBot11Adapter) {
|
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.coreContext = coreContext;
|
||||||
this.onebotContext = onebotContext;
|
this.obContext = onebotContext;
|
||||||
|
this.logger = coreContext.context.logger;
|
||||||
}
|
}
|
||||||
|
|
||||||
registerActionMap(actionMap: Map<string, BaseAction<any, any>>) {
|
registerActionMap(actionMap: Map<string, BaseAction<any, any>>) {
|
||||||
this.actionMap = actionMap;
|
this.actionMap = actionMap;
|
||||||
}
|
}
|
||||||
registerHeartBeat() {
|
|
||||||
if (this.connection) {
|
|
||||||
this.heartbeatTimer = setInterval(() => {
|
|
||||||
if (this.connection && this.connection.readyState === NodeWebSocket.OPEN) {
|
|
||||||
this.connection.send(JSON.stringify(new OB11HeartbeatEvent(this.coreContext, this.heartbeatInterval, this.coreContext.selfInfo.online, true)));
|
|
||||||
}
|
|
||||||
}, this.heartbeatInterval);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
registerAction<T extends BaseAction<P, R>, P, R>(action: T) {
|
registerAction<T extends BaseAction<P, R>, P, R>(action: T) {
|
||||||
this.actionMap.set(action.actionName, action);
|
this.actionMap.set(action.actionName, action);
|
||||||
@ -69,6 +66,22 @@ export class OB11ActiveWebSocketAdapter implements IOB11NetworkAdapter {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private registerHeartBeat() {
|
||||||
|
if (this.connection) {
|
||||||
|
this.heartbeatTimer = setInterval(() => {
|
||||||
|
if (this.connection && this.connection.readyState === NodeWebSocket.OPEN) {
|
||||||
|
this.connection.send(JSON.stringify(new OB11HeartbeatEvent(this.coreContext, this.heartbeatInterval, this.coreContext.selfInfo.online, true)));
|
||||||
|
}
|
||||||
|
}, this.heartbeatInterval);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
private checkStateAndReply<T>(data: T) {
|
||||||
|
if (this.connection && this.connection.readyState === WebSocket.OPEN) {
|
||||||
|
this.connection.send(JSON.stringify(data));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
private async tryConnect() {
|
private async tryConnect() {
|
||||||
while (!this.connection && !this.isClosed) {
|
while (!this.connection && !this.isClosed) {
|
||||||
try {
|
try {
|
||||||
@ -91,15 +104,24 @@ export class OB11ActiveWebSocketAdapter implements IOB11NetworkAdapter {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
private handleMessage(data: any) {
|
private async handleMessage(message: any) {
|
||||||
|
let receiveData: { action: ActionName, params?: any, echo?: any } = { action: ActionName.Unknown, params: {} };
|
||||||
|
let echo = undefined;
|
||||||
try {
|
try {
|
||||||
const message = JSON.parse(data);
|
try {
|
||||||
const action = this.actionMap.get(message.actionName);
|
receiveData = JSON.parse(message.toString());
|
||||||
if (action) {
|
echo = receiveData.echo;
|
||||||
action.handle(message.payload);
|
this.logger.logDebug('收到正向Websocket消息', receiveData);
|
||||||
}
|
|
||||||
} catch (e) {
|
} catch (e) {
|
||||||
console.error('Failed to handle message:', e);
|
this.checkStateAndReply<any>(OB11Response.error('json解析失败,请检查数据格式', 1400, echo));
|
||||||
|
}
|
||||||
|
receiveData.params = (receiveData?.params) ? receiveData.params : {};//兼容类型验证
|
||||||
|
const retdata = await this.actionMap.get(receiveData.action)
|
||||||
|
?.websocketHandle(receiveData.params, echo || '');
|
||||||
|
const packet = Object.assign({}, retdata);
|
||||||
|
this.checkStateAndReply<any>(packet);
|
||||||
|
} catch (e) {
|
||||||
|
this.checkStateAndReply<any>(OB11Response.error('不支持的api ' + receiveData.action, 1404, echo));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
Loading…
x
Reference in New Issue
Block a user