fix: send packet

This commit is contained in:
手瓜一十雪 2024-10-12 15:18:20 +08:00
parent 69c477b104
commit eba5900ba8
4 changed files with 20 additions and 13 deletions

View File

@ -22,15 +22,20 @@ export class NTQQPacketApi {
constructor(context: InstanceContext, core: NapCatCore) {
this.context = context;
this.core = core;
this.InitSendPacket('127.0.0.1:8086', '9.9.15-28418', '1001').then().catch(this.core.context.logger.logError.bind(this.core.context.logger));
let config = this.core.configLoader.configData;
if (config && config.packetServer && config.packetServer.length > 0) {
let serverurl = this.core.configLoader.configData.packetServer ?? '127.0.0.1:8086';
this.InitSendPacket(serverurl, this.context.basicInfoWrapper.getFullQQVesion())
.then()
.catch(this.core.context.logger.logError.bind(this.core.context.logger));
}
}
async InitSendPacket(serverUrl: string, qqversion: string, uin: string) {
async InitSendPacket(serverUrl: string, qqversion: string) {
this.serverUrl = serverUrl;
this.qqversion = qqversion;
let offsetTable: OffsetType = offset;
if (!offsetTable[qqversion]) return false;
let url = 'ws://' + this.serverUrl + '/ws';
// let postdata = { recv: offsetTable[qqversion].recv, send: offsetTable[qqversion].send, qqver: qqversion, uin: uin, pid: process.pid };
this.PacketClient = new PacketClient(url, this.core.context.logger);
await this.PacketClient.connect();
await this.PacketClient.init(process.pid, offsetTable[qqversion].recv, offsetTable[qqversion].send);

View File

@ -2,5 +2,6 @@
"fileLog": true,
"consoleLog": true,
"fileLogLevel": "debug",
"consoleLogLevel": "info"
}
"consoleLogLevel": "info",
"packetServer": ""
}

View File

@ -14,7 +14,7 @@ export class PacketClient {
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;
@ -45,7 +45,7 @@ export class PacketClient {
if (this.reconnectAttempts < this.maxReconnectAttempts) {
this.reconnectAttempts++;
this.logger.logError.bind(this.logger)(`Reconnecting attempt ${this.reconnectAttempts}`);
setTimeout(() => this.connect(), 1000 * this.reconnectAttempts);
setTimeout(() => this.connect().then().catch(), 1000 * this.reconnectAttempts);
} else {
this.logger.logError.bind(this.logger)(`Max reconnect attempts reached. Could not reconnect to ${this.url}`);
}
@ -81,16 +81,17 @@ export class PacketClient {
};
this.websocket.send(JSON.stringify(commandMessage));
if (rsp) {
this.registerCallback(trace_id, 'recv', (json: any) => {
clearTimeout(timeoutHandle);
resolve(json);
});
}
this.registerCallback(trace_id, 'send', (json: any) => {
sendcb(json);
if (!rsp) {
clearTimeout(timeoutHandle);
resolve(json);
} else {
this.registerCallback(trace_id, 'recv', (json: any) => {
clearTimeout(timeoutHandle);
resolve(json);
});
}
});
const timeoutHandle = setTimeout(() => {

View File

@ -81,6 +81,7 @@ export class NapCatCore {
this.context = context;
this.util = this.context.wrapper.NodeQQNTWrapperUtil;
this.eventWrapper = new NTEventWrapper(context.session);
this.configLoader = new NapCatConfigLoader(this, this.context.pathWrapper.configPath);
this.apis = {
FileApi: new NTQQFileApi(this.context, this),
SystemApi: new NTQQSystemApi(this.context, this),
@ -92,7 +93,6 @@ export class NapCatCore {
UserApi: new NTQQUserApi(this.context, this),
GroupApi: new NTQQGroupApi(this.context, this),
};
this.configLoader = new NapCatConfigLoader(this, this.context.pathWrapper.configPath);
this.NapCatDataPath = path.join(this.dataPath, 'NapCat');
fs.mkdirSync(this.NapCatDataPath, { recursive: true });
this.NapCatTempPath = path.join(this.NapCatDataPath, 'temp');