From 60b62f1511fbcf384311eb68eb21a40fc28f0101 Mon Sep 17 00:00:00 2001 From: thearturca Date: Tue, 29 Oct 2024 20:42:03 +0300 Subject: [PATCH] refactor(centrifugo): moves pingPong to its own listener --- src/services/CentrifugeClient.ts | 15 +++++++++++---- 1 file changed, 11 insertions(+), 4 deletions(-) diff --git a/src/services/CentrifugeClient.ts b/src/services/CentrifugeClient.ts index e3ddac5..a3b2e73 100644 --- a/src/services/CentrifugeClient.ts +++ b/src/services/CentrifugeClient.ts @@ -27,9 +27,7 @@ export class CentrifugeClient< public connect(): Promise { return new Promise((resolve, reject) => { - if (this.socket) { - this.socket.close(); - } + this.disconnect(); this.socket = new WebSocket(this.wsServerUrl, { headers: { Origin: "https://live.vkplay.ru" }, @@ -39,6 +37,16 @@ export class CentrifugeClient< await this.onOpen(e).catch(reject); resolve(); }; + this.socket.on("message", (data) => { + if (data.toString("utf8") === "{}") { + this.socket?.send("{}", (error) => { + if (error) { + console.error("[error:websocket]", error); + } + }); + return; + } + }); this.socket.onmessage = (e): void => this.onMessage(e); this.socket.onclose = (e): Promise => this.onClose(e); this.socket.onerror = (e): void => this.onError(e); @@ -180,7 +188,6 @@ export class CentrifugeClient< public onMessage(event: WebSocket.MessageEvent): void { if (event.data === "{}") { - this.socket?.send("{}"); return; }