mirror of
https://github.com/NapNeko/NapCatQQ.git
synced 2024-11-21 09:36:35 +00:00
refactor: add available
accessor property in PacketClient
This commit is contained in:
parent
b4cb3ddf1c
commit
a79c933693
@ -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();
|
||||
}
|
||||
|
@ -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;
|
||||
}
|
||||
|
@ -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<string, any>(500);
|
||||
constructor(private url: string, public logger: LogWrapper) { }
|
||||
|
||||
get available(): boolean {
|
||||
return this.isConnected && this.websocket !== undefined;
|
||||
}
|
||||
|
||||
connect(): Promise<void> {
|
||||
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;
|
||||
|
@ -15,7 +15,7 @@ export class GetRkey extends BaseAction<Payload, Array<any>> {
|
||||
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();
|
||||
|
@ -17,7 +17,7 @@ export class GetUserStatus extends BaseAction<Payload, { status: number; ext_sta
|
||||
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.sendStatusPacket(+payload.user_id);
|
||||
|
@ -18,7 +18,7 @@ export class SetSpecialTittle extends BaseAction<Payload, any> {
|
||||
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());
|
||||
|
@ -20,7 +20,7 @@ export class UploadForwardMsg extends BaseAction<Payload, any> {
|
||||
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');
|
||||
|
@ -18,7 +18,7 @@ export class GroupPoke extends BaseAction<Payload, any> {
|
||||
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);
|
||||
|
Loading…
x
Reference in New Issue
Block a user