From be91976498389c8669ffbf7931ef3d1d0fae3439 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E6=89=8B=E7=93=9C=E4=B8=80=E5=8D=81=E9=9B=AA?= Date: Sun, 11 Aug 2024 23:10:56 +0800 Subject: [PATCH] =?UTF-8?q?chore:=20=E5=88=9D=E6=AD=A5=E5=AE=8C=E6=88=90?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/onebot/network/active-websocket.ts | 33 +++++++++++++++++++------- 1 file changed, 25 insertions(+), 8 deletions(-) diff --git a/src/onebot/network/active-websocket.ts b/src/onebot/network/active-websocket.ts index 34bc117e..678da3e6 100644 --- a/src/onebot/network/active-websocket.ts +++ b/src/onebot/network/active-websocket.ts @@ -5,6 +5,9 @@ import { sleep } from '@/common/utils/helper'; import { OB11HeartbeatEvent } from '../event/meta/OB11HeartbeatEvent'; import { NapCatCore } from '@/core'; import { NapCatOneBot11Adapter } from '../main'; +import { OB11Response } from '../action/OB11Response'; +import { LogWrapper } from '@/common/utils/log'; +import { ActionName } from '../action/types'; export class OB11ActiveWebSocketAdapter implements IOB11NetworkAdapter { url: string; @@ -17,9 +20,11 @@ export class OB11ActiveWebSocketAdapter implements IOB11NetworkAdapter { onebotContext: NapCatOneBot11Adapter; coreContext: NapCatCore; token: string; + logger: LogWrapper; - constructor(url: string, reconnectIntervalInMillis: number, heartbeatInterval: number, token:string, coreContext: NapCatCore, onebotContext: NapCatOneBot11Adapter) { + constructor(url: string, reconnectIntervalInMillis: number, heartbeatInterval: number, token: string, coreContext: NapCatCore, onebotContext: NapCatOneBot11Adapter) { this.url = url; + this.logger = coreContext.context.logger; this.token = token; this.heartbeatInterval = heartbeatInterval; this.reconnectIntervalInMillis = reconnectIntervalInMillis; @@ -99,16 +104,28 @@ export class OB11ActiveWebSocketAdapter implements IOB11NetworkAdapter { } } } - - private handleMessage(data: any) { + WsReply(data: any) { + if (this.connection?.readyState === NodeWebSocket.OPEN) { + this.connection?.send(JSON.stringify(data)); + } + } + private async handleMessage(message: any) { + let receiveData: { action: ActionName, params?: any, echo?: any } = { action: ActionName.Unknown, params: {} }; + let echo = undefined; try { - const message = JSON.parse(data); - const action = this.actionMap.get(message.actionName); - if (action) { - action.handle(message.payload); + try { + receiveData = JSON.parse(message.toString()); + echo = receiveData.echo; + this.logger.logDebug('收到正向Websocket消息', receiveData); + } catch (e) { + this.WsReply(OB11Response.error('json解析失败,请检查数据格式', 1400, echo)); } + receiveData.params = (receiveData?.params) ? receiveData.params : {};//兼容类型验证 + let retdata = await this.actionMap.get(receiveData.action)?.websocketHandle(receiveData.params, echo || ""); + const packet = Object.assign({}, retdata); + this.WsReply(packet); } catch (e) { - console.error('Failed to handle message:', e); + this.WsReply(OB11Response.error('不支持的api ' + receiveData.action, 1404, echo)); } } } \ No newline at end of file