mirror of
https://github.com/NapNeko/NapCatQQ.git
synced 2025-07-19 12:03:37 +00:00
refactor: further decoupling of Packet
and Core
parts
This commit is contained in:
@@ -1,7 +1,9 @@
|
||||
import {LogWrapper} from "@/common/log";
|
||||
import {LRUCache} from "@/common/lru-cache";
|
||||
import WebSocket, {Data} from "ws";
|
||||
import {createHash} from "crypto";
|
||||
import crypto, {createHash} from "crypto";
|
||||
import {NapCatCore} from "@/core";
|
||||
import {PacketHexStr} from "@/core/packet/packer";
|
||||
|
||||
export interface RecvPacket {
|
||||
type: string, // 仅recv
|
||||
@@ -22,17 +24,28 @@ export class PacketClient {
|
||||
private maxReconnectAttempts: number = 5;
|
||||
private cb = new LRUCache<string, (json: RecvPacketData) => Promise<void>>(500); // trace_id-type callback
|
||||
private readonly clientUrl: string = '';
|
||||
private readonly napCatCore: NapCatCore
|
||||
private readonly logger: LogWrapper;
|
||||
|
||||
constructor(url: string, logger: LogWrapper) {
|
||||
constructor(url: string, core: NapCatCore) {
|
||||
this.clientUrl = url;
|
||||
this.logger = logger;
|
||||
this.napCatCore = core;
|
||||
this.logger = core.context.logger;
|
||||
}
|
||||
|
||||
get available(): boolean {
|
||||
return this.isConnected && this.websocket !== undefined;
|
||||
}
|
||||
|
||||
private randText(len: number) {
|
||||
let text = '';
|
||||
let possible = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789';
|
||||
for (let i = 0; i < len; i++) {
|
||||
text += possible.charAt(Math.floor(Math.random() * possible.length));
|
||||
}
|
||||
return text;
|
||||
}
|
||||
|
||||
connect(): Promise<void> {
|
||||
return new Promise((resolve, reject) => {
|
||||
this.logger.log.bind(this.logger)(`Attempting to connect to ${this.clientUrl}`);
|
||||
@@ -91,7 +104,6 @@ export class PacketClient {
|
||||
if (!this.isConnected || !this.websocket) {
|
||||
throw new Error("WebSocket is not connected");
|
||||
}
|
||||
|
||||
const initMessage = {
|
||||
action: 'init',
|
||||
pid: pid,
|
||||
@@ -147,4 +159,20 @@ export class PacketClient {
|
||||
this.logger.logError.bind(this.logger)(`Error parsing message: ${error}`);
|
||||
}
|
||||
}
|
||||
|
||||
async sendPacket(cmd: string, data: PacketHexStr, rsp = false): Promise<RecvPacketData> {
|
||||
// wtfk tx
|
||||
// 校验失败和异常 可能返回undefined
|
||||
return new Promise((resolve, reject) => {
|
||||
if (!this.available) {
|
||||
this.logger.logError('NapCat.Packet is not init');
|
||||
return undefined;
|
||||
}
|
||||
let md5 = crypto.createHash('md5').update(data).digest('hex');
|
||||
let trace_id = (this.randText(4) + md5 + data).slice(0, data.length / 2);
|
||||
this.sendCommand(cmd, data, trace_id, rsp, 5000, async () => {
|
||||
await this.napCatCore.context.session.getMsgService().sendSsoCmdReqByContend(cmd, trace_id);
|
||||
}).then((res) => resolve(res)).catch((e) => reject(e));
|
||||
});
|
||||
}
|
||||
}
|
||||
|
15
src/core/packet/session.ts
Normal file
15
src/core/packet/session.ts
Normal file
@@ -0,0 +1,15 @@
|
||||
import {PacketClient} from "@/core/packet/client";
|
||||
import {PacketHighwayClient} from "@/core/packet/highway/highwayClient";
|
||||
import {LogWrapper} from "@/common/log";
|
||||
|
||||
export class PacketSession {
|
||||
readonly logger: LogWrapper;
|
||||
readonly client: PacketClient;
|
||||
private highwayClient: PacketHighwayClient
|
||||
|
||||
constructor(logger: LogWrapper, client: PacketClient) {
|
||||
this.logger = logger;
|
||||
this.client = client;
|
||||
this.highwayClient = new PacketHighwayClient(this.logger, this.client);
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user