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) { constructor(context: InstanceContext, core: NapCatCore) {
this.context = context; this.context = context;
this.core = core; 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.serverUrl = serverUrl;
this.qqversion = qqversion; this.qqversion = qqversion;
let offsetTable: OffsetType = offset; let offsetTable: OffsetType = offset;
if (!offsetTable[qqversion]) return false; if (!offsetTable[qqversion]) return false;
let url = 'ws://' + this.serverUrl + '/ws'; 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); this.PacketClient = new PacketClient(url, this.core.context.logger);
await this.PacketClient.connect(); await this.PacketClient.connect();
await this.PacketClient.init(process.pid, offsetTable[qqversion].recv, offsetTable[qqversion].send); await this.PacketClient.init(process.pid, offsetTable[qqversion].recv, offsetTable[qqversion].send);

View File

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

View File

@ -14,7 +14,7 @@ export class PacketClient {
return new Promise((resolve, reject) => { return new Promise((resolve, reject) => {
this.logger.log.bind(this.logger)(`Attempting to connect to ${this.url}`); this.logger.log.bind(this.logger)(`Attempting to connect to ${this.url}`);
this.websocket = new WebSocket(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.websocket.onopen = () => {
this.isConnected = true; this.isConnected = true;
this.reconnectAttempts = 0; this.reconnectAttempts = 0;
@ -45,7 +45,7 @@ export class PacketClient {
if (this.reconnectAttempts < this.maxReconnectAttempts) { if (this.reconnectAttempts < this.maxReconnectAttempts) {
this.reconnectAttempts++; this.reconnectAttempts++;
this.logger.logError.bind(this.logger)(`Reconnecting attempt ${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 { } 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.url}`);
} }
@ -81,16 +81,17 @@ export class PacketClient {
}; };
this.websocket.send(JSON.stringify(commandMessage)); 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) => { this.registerCallback(trace_id, 'send', (json: any) => {
sendcb(json); sendcb(json);
if (!rsp) { if (!rsp) {
clearTimeout(timeoutHandle); clearTimeout(timeoutHandle);
resolve(json); resolve(json);
} else {
this.registerCallback(trace_id, 'recv', (json: any) => {
clearTimeout(timeoutHandle);
resolve(json);
});
} }
}); });
const timeoutHandle = setTimeout(() => { const timeoutHandle = setTimeout(() => {

View File

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