From 9b0e64aff187daa24122ce9b3aa70155b431de6f Mon Sep 17 00:00:00 2001 From: Marek Rusinowski Date: Fri, 3 Jan 2025 22:04:27 +0100 Subject: [PATCH] Allow TachyonClient send client to get send result This will be useful for the procedure to catchup updates after prolonged server disconnect, we want to push more messages down the socket only when we know that socket is drained to not buffer in multiple places and blow up memory. --- src/main.ts | 2 +- src/tachyonClient.ts | 15 +++++++++++---- src/tachyonTypes.ts | 4 ++-- 3 files changed, 14 insertions(+), 7 deletions(-) diff --git a/src/main.ts b/src/main.ts index 591cbfa..67f01f5 100644 --- a/src/main.ts +++ b/src/main.ts @@ -54,7 +54,7 @@ async function main(argv: string[]) { return; } if (msg.type == 'event') return; - client.send(await callTachyonAutohost(msg, autohost)); + client.send(await callTachyonAutohost(msg, autohost)).catch(() => undefined); }); try { diff --git a/src/tachyonClient.ts b/src/tachyonClient.ts index 0ed421e..2768c5c 100644 --- a/src/tachyonClient.ts +++ b/src/tachyonClient.ts @@ -79,6 +79,7 @@ export class TachyonClient extends TypedEmitter<{ headers: { authorization: `Bearer ${accessToken}`, }, + perMessageDeflate: false, }); this.ws = ws; @@ -114,13 +115,19 @@ export class TachyonClient extends TypedEmitter<{ this.close(); } - public send(msg: TachyonCommand): void { + public send(msg: TachyonCommand): Promise { if (this.state !== ClientState.CONNECTED) { throw new Error('Client is not connected'); } - if (this.ws) { - this.ws.send(JSON.stringify(msg)); - } + return new Promise((resolve, reject) => { + this.ws!.send(JSON.stringify(msg), { binary: false }, (err) => { + if (err) { + reject(err); + } else { + resolve(); + } + }); + }); } public close() { diff --git a/src/tachyonTypes.ts b/src/tachyonTypes.ts index c533724..a4038f4 100644 --- a/src/tachyonTypes.ts +++ b/src/tachyonTypes.ts @@ -102,8 +102,8 @@ export interface TachyonAutohost { * The interface that represents the functionality that the autohost can call on the server. */ export interface TachyonServer { - status(event: AutohostStatusEventData): void; - update(event: AutohostUpdateEventData): void; + status(event: AutohostStatusEventData): Promise; + update(event: AutohostUpdateEventData): Promise; } // Helper that works as a type guard to check if an element is included in an array.