Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 21 additions & 4 deletions src/events/EventClient.ts
Original file line number Diff line number Diff line change
Expand Up @@ -153,7 +153,7 @@ export class EventClient<

this.#connectTimeoutReference = setTimeout(
() => this.disconnect(),
this.options.pongTimeout * 1e3,
this.options.connectTimeout * 1e3,
) as never;

const url = new URL(uri);
Expand All @@ -178,8 +178,25 @@ export class EventClient<
this.#socket = new WebSocket(url);

this.#socket.onopen = () => {
clearInterval(this.#heartbeatIntervalReference);
clearTimeout(this.#pongTimeoutReference);
clearTimeout(this.#connectTimeoutReference);
this.#heartbeatIntervalReference = setInterval(() => {
if (!this.#socket) {
clearInterval(this.#heartbeatIntervalReference);
clearTimeout(this.#pongTimeoutReference);
return;
}
if (this.#socket.readyState === WebSocket.CONNECTING) {
return;
}
if (this.#socket.readyState !== WebSocket.OPEN) {
clearInterval(this.#heartbeatIntervalReference);
clearTimeout(this.#pongTimeoutReference);
return;
}
this.send({ type: "Ping", data: +new Date() });
clearTimeout(this.#pongTimeoutReference);
this.#pongTimeoutReference = setTimeout(
() => this.disconnect(),
this.options.pongTimeout * 1e3,
Expand All @@ -193,7 +210,7 @@ export class EventClient<
};

this.#socket.onmessage = (event) => {
clearInterval(this.#connectTimeoutReference);
clearTimeout(this.#connectTimeoutReference);

if (this.#transportFormat === "json") {
if (typeof event.data === "string") {
Expand All @@ -218,8 +235,8 @@ export class EventClient<
disconnect(): void {
if (!this.#socket) return;
clearInterval(this.#heartbeatIntervalReference);
clearInterval(this.#connectTimeoutReference);
clearInterval(this.#pongTimeoutReference);
clearTimeout(this.#connectTimeoutReference);
clearTimeout(this.#pongTimeoutReference);
const socket = this.#socket;
this.#socket = undefined;
socket.close();
Expand Down