From 7c4dd4a8f126701e2a4a63bd9d860e2068941bd7 Mon Sep 17 00:00:00 2001 From: destruc7i0n Date: Tue, 4 Feb 2020 12:57:59 -0500 Subject: Update types --- src/Rcon.ts | 33 ++++++++++++++++++++++----------- 1 file changed, 22 insertions(+), 11 deletions(-) (limited to 'src/Rcon.ts') diff --git a/src/Rcon.ts b/src/Rcon.ts index 81cf3ea..180ad38 100644 --- a/src/Rcon.ts +++ b/src/Rcon.ts @@ -14,7 +14,7 @@ class Rcon { ip: string port: number - packages: any + packages: { [key: number]: (type: number, response: string) => void } constructor (ip: string, port: number, debug: boolean) { this.ip = ip @@ -32,7 +32,7 @@ class Rcon { console.log('[INFO] Authenticated with ' + ip + ':' + port) }) - this.socket.on('data', (data) => { + this.socket.on('data', (data: Buffer) => { const id = data.readInt32LE(4) const type = data.readInt32LE(8) const response = data.toString('ascii', 12, data.length - 2) @@ -49,31 +49,42 @@ class Rcon { }) } - close () { + public close () { this.connected = false this.socket.end() } - async auth (password: string) { + public async auth (password: string): Promise { if (this.authed) { throw new Error('Already authed') } if (this.connected){ - await this.sendPackage(3, password) + try { + await this.sendPackage(3, password) + } catch (e) { + console.log('[ERROR] Could not send password to Rcon server!') + if (this.debug) console.error(e) + } } else { - return new Promise(resolve => { + return new Promise((resolve, reject) => { this.socket.on('connect', async () => { - await this.sendPackage(3, password) - resolve() + try { + await this.sendPackage(3, password) + resolve() + } catch (e) { + console.log('[ERROR] Could not send password to Rcon server!') + if (this.debug) console.error(e) + reject(e) + } }) }) } } - command (cmd: string) { + public command (cmd: string): Promise { return this.sendPackage(2, cmd) } - sendPackage (type: number, payload: string) { + public sendPackage (type: number, payload: string): Promise { const id = this.nextId this.nextId++ @@ -97,7 +108,7 @@ class Rcon { return reject('Server sent no request in ' + this.timeout / 1000 + ' seconds') }, this.timeout) - this.packages[id] = (type: number, response: any) => { + this.packages[id] = (type: number, response: string) => { clearTimeout(timeout) const err = type >= 0 ? false : 'Server sent package code ' + type if (this.debug) { -- cgit 1.4.1