diff --git a/src/core/apis/packet.ts b/src/core/apis/packet.ts index 3dff42f2..c169a574 100644 --- a/src/core/apis/packet.ts +++ b/src/core/apis/packet.ts @@ -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); diff --git a/src/core/external/napcat.json b/src/core/external/napcat.json index 1c822077..92432677 100644 --- a/src/core/external/napcat.json +++ b/src/core/external/napcat.json @@ -2,5 +2,6 @@ "fileLog": true, "consoleLog": true, "fileLogLevel": "debug", - "consoleLogLevel": "info" -} + "consoleLogLevel": "info", + "packetServer": "" +} \ No newline at end of file diff --git a/src/core/helper/packet.ts b/src/core/helper/packet.ts index 65c2e2b1..6d386aa3 100644 --- a/src/core/helper/packet.ts +++ b/src/core/helper/packet.ts @@ -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(() => { diff --git a/src/core/index.ts b/src/core/index.ts index 9d8cfba7..338753f3 100644 --- a/src/core/index.ts +++ b/src/core/index.ts @@ -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');