From 4ab9cda02902cd7569bec0f7acc8975de4bdc6be Mon Sep 17 00:00:00 2001 From: abir Date: Wed, 20 Jan 2021 23:48:42 +0300 Subject: [PATCH] fix: keep alive --- packages/ws/src/Protocol/GQL/GQLProtoLayer.ts | 8 ++++++++ 1 file changed, 8 insertions(+) diff --git a/packages/ws/src/Protocol/GQL/GQLProtoLayer.ts b/packages/ws/src/Protocol/GQL/GQLProtoLayer.ts index 4c4c4bc9..0ad85d27 100644 --- a/packages/ws/src/Protocol/GQL/GQLProtoLayer.ts +++ b/packages/ws/src/Protocol/GQL/GQLProtoLayer.ts @@ -25,7 +25,10 @@ export class GQLProtoLayer { constructor(client: GQLClientConnection, factory: GQLSubscribeFunction) { this.#client = client; this.#subscribe = factory; + + const interval = setInterval(() => this.keepAliveUpdate(), 30000); this.#client.on("close", () => this.unsubscribeAll()); + this.#client.on("close", () => clearInterval(interval)); } public async handle(operation: GQLOperationMessage): Promise { @@ -34,6 +37,7 @@ export class GQLProtoLayer { case GQLClientOperationType.CONNECTION_INIT: Object.assign(this.#params, operation.payload); await this.#client.send({type: GQLServerOperationType.CONNECTION_ACK}); + await this.keepAliveUpdate(); break; case GQLClientOperationType.CONNECTION_TERMINATE: this.terminate(); @@ -51,6 +55,10 @@ export class GQLProtoLayer { } } + private keepAliveUpdate() { + return this.#client.send({type: GQLServerOperationType.CONNECTION_KEEP_ALIVE}); + } + private unsubscribeAll() { for (const subscription of this.#subscriptions.values()) { subscription.return?.();