Skip to content

Commit

Permalink
fix: keep alive
Browse files Browse the repository at this point in the history
  • Loading branch information
izatop committed Jan 20, 2021
1 parent 3439a72 commit 4ab9cda
Showing 1 changed file with 8 additions and 0 deletions.
8 changes: 8 additions & 0 deletions packages/ws/src/Protocol/GQL/GQLProtoLayer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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<void> {
Expand All @@ -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();
Expand All @@ -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?.();
Expand Down

0 comments on commit 4ab9cda

Please sign in to comment.