diff --git a/rcon.ts b/rcon.ts index 5e3ff04..7df6d76 100644 --- a/rcon.ts +++ b/rcon.ts @@ -30,6 +30,17 @@ class ResolvablePromise { }); } +export interface Options { + /** Support multi-packet responses from the server. On by default. + * May be turned off in order to read responses from servers that + * don't support it, like Factorio. */ + multiPacketResponses: boolean; +} + +const defaultOptions: Options = { + multiPacketResponses: true +}; + export class Rcon { private authed?: ResolvablePromise; @@ -39,11 +50,16 @@ export class Rcon { private requests = new Map(); + private options: Options; + constructor( private host = 'localhost', private port = 27015, - private password = '' - ) {} + private password = '', + options: Partial = {} + ) { + this.options = {...defaultOptions, ...options}; + } sendCmd(cmd: string) { return this.send(cmd, PacketType.COMMAND); @@ -120,7 +136,11 @@ export class Rcon { const request = this.requests.get(id); if (request) { - if (equals(FrameBuffer, payload)) { + if (!this.options.multiPacketResponses) { + const str = new TextDecoder().decode(payload); + request.resolve(str); + this.requests.delete(id); + } else if (equals(FrameBuffer, payload)) { const str = new TextDecoder().decode(request.data); request.resolve(str); this.requests.delete(id);