diff --git a/packages/providers/src.ts/websocket-provider.ts b/packages/providers/src.ts/websocket-provider.ts index e447f648f..1b8958ec1 100644 --- a/packages/providers/src.ts/websocket-provider.ts +++ b/packages/providers/src.ts/websocket-provider.ts @@ -274,4 +274,22 @@ export class WebSocketProvider extends JsonRpcProvider { this.send("eth_unsubscribe", [ subId ]); }); } + + async destroy(): Promise { + // Wait until we have connected before trying to disconnect + if (this._websocket.readyState === WebSocket.CONNECTING) { + await new Promise((resolve) => { + this._websocket.on("open", () => { + resolve(true); + }); + + this._websocket.on("error", () => { + resolve(false); + }); + }); + } + + // Hangup (navigating away from the page that opened the connection) + this._websocket.close(1001); + } }