Skip to content

Commit

Permalink
refactor(centrifugo): moves pingPong to its own listener
Browse files Browse the repository at this point in the history
  • Loading branch information
thearturca committed Oct 29, 2024
1 parent 04e043c commit 60b62f1
Showing 1 changed file with 11 additions and 4 deletions.
15 changes: 11 additions & 4 deletions src/services/CentrifugeClient.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,9 +27,7 @@ export class CentrifugeClient<

public connect(): Promise<void> {
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" },
Expand All @@ -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<void> => this.onClose(e);
this.socket.onerror = (e): void => this.onError(e);
Expand Down Expand Up @@ -180,7 +188,6 @@ export class CentrifugeClient<

public onMessage(event: WebSocket.MessageEvent): void {
if (event.data === "{}") {
this.socket?.send("{}");
return;
}

Expand Down

0 comments on commit 60b62f1

Please sign in to comment.