Skip to content

Handle WebSocket close and reconnect in WebSocketProvider #4976

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
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
65 changes: 50 additions & 15 deletions lib.esm/providers/provider-websocket.js
Original file line number Diff line number Diff line change
@@ -13,6 +13,8 @@ import { SocketProvider } from "./provider-socket.js";
export class WebSocketProvider extends SocketProvider {
#connect;
#websocket;
#pingTimeout;
#keepAliveInterval;
get websocket() {
if (this.#websocket == null) {
throw new Error("websocket closed");
@@ -37,6 +39,7 @@ export class WebSocketProvider extends SocketProvider {
try {
await this._start();
this.resume();
this.#startKeepAlive();
}
catch (error) {
console.log("failed to start WebsocketProvider", error);
@@ -46,31 +49,63 @@ export class WebSocketProvider extends SocketProvider {
this.websocket.onmessage = (message) => {
this._processMessage(message.data);
};
/*
this.websocket.onclose = (event) => {
// @TODO: What event.code should we reconnect on?
const reconnect = false;
if (reconnect) {
this.pause(true);
if (this.#connect) {
this.#websocket = this.#connect();
this.#websocket.onopen = ...
// @TODO: this requires the super class to rebroadcast; move it there
}
this._reconnect();
this.websocket.onclose = (event) => {
this.#stopKeepAlive();
this.pause(true);
if (this.#connect) {
this.#websocket = this.#connect();
this.#websocket.onopen = this.websocket.onopen;
this.#websocket.onmessage = this.websocket.onmessage;
this.#websocket.onclose = this.websocket.onclose;
this.#websocket.onping = this.websocket.onping;
this.#websocket.onpong = this.websocket.onpong;
}
};
this.websocket.onping = () => {
if (this.#pingTimeout) {
clearTimeout(this.#pingTimeout);
this.#pingTimeout = null;
}
this.websocket.pong();
};
this.websocket.onpong = () => {
if (this.#pingTimeout) {
clearTimeout(this.#pingTimeout);
this.#pingTimeout = null;
}
};
}
#startKeepAlive() {
this.#keepAliveInterval = setInterval(() => {
if (this.#websocket && this.#websocket.readyState === 1) {
this.#websocket.ping();
this.#pingTimeout = setTimeout(() => {
if (this.#websocket) {
this.#websocket.close();
}
};
*/
}, 15000);
}
}, 7500);
}
#stopKeepAlive() {
if (this.#keepAliveInterval) {
clearInterval(this.#keepAliveInterval);
this.#keepAliveInterval = null;
}
if (this.#pingTimeout) {
clearTimeout(this.#pingTimeout);
this.#pingTimeout = null;
}
}
async _write(message) {
this.websocket.send(message);
}
async destroy() {
this.#stopKeepAlive();
if (this.#websocket != null) {
this.#websocket.close();
this.#websocket = null;
}
super.destroy();
}
}
//# sourceMappingURL=provider-websocket.js.map
72 changes: 58 additions & 14 deletions src.ts/providers/provider-websocket.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@


import { WebSocket as _WebSocket } from "./ws.js"; /*-browser*/

import { SocketProvider } from "./provider-socket.js";
@@ -14,11 +12,15 @@ export interface WebSocketLike {
onopen: null | ((...args: Array<any>) => any);
onmessage: null | ((...args: Array<any>) => any);
onerror: null | ((...args: Array<any>) => any);
onclose: null | ((...args: Array<any>) => any);
onping: null | ((...args: Array<any>) => any);
onpong: null | ((...args: Array<any>) => any);

readyState: number;

send(payload: any): void;
close(code?: number, reason?: string): void;
ping(): void;
}

/**
@@ -46,6 +48,9 @@ export class WebSocketProvider extends SocketProvider {
return this.#websocket;
}

#pingTimeout: null | ReturnType<typeof setTimeout>;
#keepAliveInterval: null | ReturnType<typeof setInterval>;

constructor(url: string | WebSocketLike | WebSocketCreator, network?: Networkish, options?: JsonRpcApiProviderOptions) {
super(network, options);
if (typeof(url) === "string") {
@@ -63,6 +68,7 @@ export class WebSocketProvider extends SocketProvider {
try {
await this._start()
this.resume();
this.#startKeepAlive();
} catch (error) {
console.log("failed to start WebsocketProvider", error);
// @TODO: now what? Attempt reconnect?
@@ -72,28 +78,66 @@ export class WebSocketProvider extends SocketProvider {
this.websocket.onmessage = (message: { data: string }) => {
this._processMessage(message.data);
};
/*

this.websocket.onclose = (event) => {
// @TODO: What event.code should we reconnect on?
const reconnect = false;
if (reconnect) {
this.pause(true);
if (this.#connect) {
this.#websocket = this.#connect();
this.#websocket.onopen = ...
// @TODO: this requires the super class to rebroadcast; move it there
}
this._reconnect();
this.#stopKeepAlive();
this.pause(true);
if (this.#connect) {
this.#websocket = this.#connect();
this.#websocket.onopen = this.websocket.onopen;
this.#websocket.onmessage = this.websocket.onmessage;
this.#websocket.onclose = this.websocket.onclose;
this.#websocket.onping = this.websocket.onping;
this.#websocket.onpong = this.websocket.onpong;
}
};
*/

this.websocket.onping = () => {
if (this.#pingTimeout) {
clearTimeout(this.#pingTimeout);
this.#pingTimeout = null;
}
this.websocket.pong();
};

this.websocket.onpong = () => {
if (this.#pingTimeout) {
clearTimeout(this.#pingTimeout);
this.#pingTimeout = null;
}
};
}

#startKeepAlive() {
this.#keepAliveInterval = setInterval(() => {
if (this.#websocket && this.#websocket.readyState === 1) {
this.#websocket.ping();
this.#pingTimeout = setTimeout(() => {
if (this.#websocket) {
this.#websocket.close();
}
}, 15000);
}
}, 7500);
}

#stopKeepAlive() {
if (this.#keepAliveInterval) {
clearInterval(this.#keepAliveInterval);
this.#keepAliveInterval = null;
}
if (this.#pingTimeout) {
clearTimeout(this.#pingTimeout);
this.#pingTimeout = null;
}
}

async _write(message: string): Promise<void> {
this.websocket.send(message);
}

async destroy(): Promise<void> {
this.#stopKeepAlive();
if (this.#websocket != null) {
this.#websocket.close();
this.#websocket = null;