mirror of
https://github.com/NapNeko/NapCatQQ.git
synced 2025-07-19 12:03:37 +00:00
chore: format & minor refactor
This commit is contained in:
@@ -116,6 +116,7 @@ export class NTQQPacketApi {
|
||||
return undefined
|
||||
}
|
||||
}
|
||||
|
||||
async sendSetSpecialTittlePacket(groupCode: string, uid: string, tittle: string) {
|
||||
let data = this.packetPacker.packSetSpecialTittlePacket(groupCode, uid, tittle);
|
||||
let ret = await this.sendPacket('OidbSvcTrpcTcp.0x8fc_2', data, true);
|
||||
|
@@ -8,9 +8,14 @@ export class PacketClient {
|
||||
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) { }
|
||||
private cb = new LRUCache<string, any>(500); // trace_id-type callback
|
||||
private readonly clientUrl: string = '';
|
||||
private readonly logger: LogWrapper;
|
||||
|
||||
constructor(url: string, logger: LogWrapper) {
|
||||
this.clientUrl = url;
|
||||
this.logger = logger;
|
||||
}
|
||||
|
||||
get available(): boolean {
|
||||
return this.isConnected && this.websocket !== undefined;
|
||||
@@ -18,14 +23,14 @@ export class PacketClient {
|
||||
|
||||
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.logger.log.bind(this.logger)(`Attempting to connect to ${this.clientUrl}`);
|
||||
this.websocket = new WebSocket(this.clientUrl);
|
||||
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;
|
||||
this.logger.log.bind(this.logger)(`Connected to ${this.url}`);
|
||||
this.logger.log.bind(this.logger)(`Connected to ${this.clientUrl}`);
|
||||
resolve();
|
||||
};
|
||||
|
||||
@@ -42,7 +47,7 @@ export class PacketClient {
|
||||
|
||||
this.websocket.onclose = () => {
|
||||
this.isConnected = false;
|
||||
this.logger.logWarn.bind(this.logger)(`Disconnected from ${this.url}`);
|
||||
this.logger.logWarn.bind(this.logger)(`Disconnected from ${this.clientUrl}`);
|
||||
this.attemptReconnect();
|
||||
};
|
||||
});
|
||||
@@ -59,12 +64,13 @@ export class PacketClient {
|
||||
});
|
||||
}, 5000 * this.reconnectAttempts);
|
||||
} else {
|
||||
this.logger.logError.bind(this.logger)(`Max reconnect attempts reached. Could not reconnect to ${this.url}`);
|
||||
this.logger.logError.bind(this.logger)(`Max reconnect attempts reached. Could not reconnect to ${this.clientUrl}`);
|
||||
}
|
||||
} catch (error: any) {
|
||||
this.logger.logError.bind(this.logger)(`Error attempting to reconnect: ${error.message}`);
|
||||
}
|
||||
}
|
||||
|
||||
async registerCallback(trace_id: string, type: string, callback: any): Promise<void> {
|
||||
this.cb.put(createHash('md5').update(trace_id).digest('hex') + type, callback);
|
||||
}
|
||||
@@ -83,7 +89,8 @@ export class PacketClient {
|
||||
this.websocket.send(JSON.stringify(initMessage));
|
||||
}
|
||||
|
||||
async sendCommand(cmd: string, data: string, trace_id: string, rsp: boolean = false, timeout: number = 5000, sendcb: any = () => { }): Promise<any> {
|
||||
async sendCommand(cmd: string, data: string, trace_id: string, rsp: boolean = false, timeout: number = 5000, sendcb: any = () => {
|
||||
}): Promise<any> {
|
||||
return new Promise<any>((resolve, reject) => {
|
||||
if (!this.isConnected || !this.websocket) {
|
||||
throw new Error("WebSocket is not connected");
|
||||
@@ -113,9 +120,9 @@ export class PacketClient {
|
||||
}, timeout);
|
||||
});
|
||||
}
|
||||
|
||||
private async handleMessage(message: any): Promise<void> {
|
||||
try {
|
||||
|
||||
let json = JSON.parse(message.toString());
|
||||
let trace_id_md5 = json.trace_id_md5;
|
||||
let action = json?.type ?? 'init';
|
||||
|
Reference in New Issue
Block a user