From a79c933693ebe60f92621495a923a964a04ddfe0 Mon Sep 17 00:00:00 2001 From: pk5ls20 Date: Mon, 14 Oct 2024 15:13:44 +0800 Subject: [PATCH] refactor: add `available` accessor property in `PacketClient` --- src/core/apis/file.ts | 2 +- src/core/apis/packet.ts | 2 +- src/core/packet/packetClient.ts | 8 ++++++-- src/onebot/action/extends/GetRkey.ts | 2 +- src/onebot/action/extends/GetUserStatus.ts | 2 +- src/onebot/action/extends/SetSpecialTittle.ts | 2 +- src/onebot/action/extends/UploadForwardMsg.ts | 2 +- src/onebot/action/group/GroupPoke.ts | 2 +- 8 files changed, 13 insertions(+), 9 deletions(-) diff --git a/src/core/apis/file.ts b/src/core/apis/file.ts index eacee189..c0457727 100644 --- a/src/core/apis/file.ts +++ b/src/core/apis/file.ts @@ -377,7 +377,7 @@ export class NTQQFileApi { online_rkey: false }; try { - if (this.core.apis.PacketApi.packetClient?.isConnected) { + if (this.core.apis.PacketApi.packetClient?.available) { if ((!this.packetRkey || this.packetRkey[0].time > Date.now() / 1000)) { this.packetRkey = await this.core.apis.PacketApi.sendRkeyPacket(); } diff --git a/src/core/apis/packet.ts b/src/core/apis/packet.ts index 0c5a8091..a9093c91 100644 --- a/src/core/apis/packet.ts +++ b/src/core/apis/packet.ts @@ -69,7 +69,7 @@ export class NTQQPacketApi { // wtfk tx // 校验失败和异常 可能返回undefined return new Promise((resolve, reject) => { - if (!this.isInit || !this.packetClient?.isConnected) { + if (!this.isInit || !this.packetClient?.available) { this.core.context.logger.logError('packetClient is not init'); return undefined; } diff --git a/src/core/packet/packetClient.ts b/src/core/packet/packetClient.ts index 86ed65aa..0cd258ac 100644 --- a/src/core/packet/packetClient.ts +++ b/src/core/packet/packetClient.ts @@ -5,19 +5,23 @@ import { createHash } from "crypto"; export class PacketClient { private websocket: WebSocket | undefined; - public isConnected: boolean = false; + private isConnected: boolean = false; private reconnectAttempts: number = 0; private maxReconnectAttempts: number = 5; //trace_id-type callback private cb = new LRUCache(500); constructor(private url: string, public logger: LogWrapper) { } + get available(): boolean { + return this.isConnected && this.websocket !== undefined; + } + connect(): Promise { return new Promise((resolve, reject) => { this.logger.log.bind(this.logger)(`Attempting to connect to ${this.url}`); this.websocket = new WebSocket(this.url); this.websocket.on('error', (err) => this.logger.logError.bind(this.logger)('[Core] [Packet Server] Error:', err.message)); - + this.websocket.onopen = () => { this.isConnected = true; this.reconnectAttempts = 0; diff --git a/src/onebot/action/extends/GetRkey.ts b/src/onebot/action/extends/GetRkey.ts index f4cb4ae3..0f18d166 100644 --- a/src/onebot/action/extends/GetRkey.ts +++ b/src/onebot/action/extends/GetRkey.ts @@ -15,7 +15,7 @@ export class GetRkey extends BaseAction> { payloadSchema = SchemaData; async _handle(payload: Payload) { - if (!this.core.apis.PacketApi.packetClient?.isConnected) { + if (!this.core.apis.PacketApi.packetClient?.available) { throw new Error('PacketClient is not init'); } return await this.core.apis.PacketApi.sendRkeyPacket(); diff --git a/src/onebot/action/extends/GetUserStatus.ts b/src/onebot/action/extends/GetUserStatus.ts index 05660238..b2d5ebd5 100644 --- a/src/onebot/action/extends/GetUserStatus.ts +++ b/src/onebot/action/extends/GetUserStatus.ts @@ -17,7 +17,7 @@ export class GetUserStatus extends BaseAction { payloadSchema = SchemaData; async _handle(payload: Payload) { - if (!this.core.apis.PacketApi.packetClient?.isConnected) { + if (!this.core.apis.PacketApi.packetClient?.available) { throw new Error('PacketClient is not init'); } let uid = await this.core.apis.UserApi.getUidByUinV2(payload.user_id.toString()); diff --git a/src/onebot/action/extends/UploadForwardMsg.ts b/src/onebot/action/extends/UploadForwardMsg.ts index 23cfa021..e0d5625d 100644 --- a/src/onebot/action/extends/UploadForwardMsg.ts +++ b/src/onebot/action/extends/UploadForwardMsg.ts @@ -20,7 +20,7 @@ export class UploadForwardMsg extends BaseAction { payloadSchema = SchemaData; async _handle(payload: Payload) { - if (!this.core.apis.PacketApi.packetClient?.isConnected) { + if (!this.core.apis.PacketApi.packetClient?.available) { throw new Error('PacketClient is not init'); } throw new Error('Not implemented'); diff --git a/src/onebot/action/group/GroupPoke.ts b/src/onebot/action/group/GroupPoke.ts index 8296afae..f58e8910 100644 --- a/src/onebot/action/group/GroupPoke.ts +++ b/src/onebot/action/group/GroupPoke.ts @@ -18,7 +18,7 @@ export class GroupPoke extends BaseAction { payloadSchema = SchemaData; async _handle(payload: Payload) { - if (!this.core.apis.PacketApi.packetClient?.isConnected) { + if (!this.core.apis.PacketApi.packetClient?.available) { throw new Error('packetClient is not init'); } await this.core.apis.PacketApi.sendPokePacket(+payload.group_id, +payload.user_id);