Initial signature change to support custom WebSockets for reconnecting providers.
This commit is contained in:
parent
db308812b2
commit
87e1f52702
@ -17,24 +17,34 @@ export interface WebSocketLike {
|
||||
close(code?: number, reason?: string): void;
|
||||
}
|
||||
|
||||
export type WebSocketCreator = () => WebSocketLike;
|
||||
|
||||
export class WebSocketProvider extends SocketProvider {
|
||||
#connect: null | WebSocketCreator;
|
||||
|
||||
#websocket: null | WebSocketLike;
|
||||
get websocket(): WebSocketLike {
|
||||
if (this.#websocket == null) { throw new Error("websocket closed"); }
|
||||
return this.#websocket;
|
||||
}
|
||||
|
||||
constructor(url: string | WebSocketLike, network?: Networkish) {
|
||||
constructor(url: string | WebSocketLike | WebSocketCreator, network?: Networkish) {
|
||||
super(network);
|
||||
if (typeof(url) === "string") {
|
||||
this.#websocket = new _WebSocket(url);
|
||||
this.#connect = () => { return new _WebSocket(url); };
|
||||
this.#websocket = this.#connect();
|
||||
} else if (typeof(url) === "function") {
|
||||
this.#connect = url;
|
||||
this.#websocket = url();
|
||||
} else {
|
||||
this.#connect = null;
|
||||
this.#websocket = url;
|
||||
}
|
||||
|
||||
this.websocket.onopen = async () => {
|
||||
try {
|
||||
await this._start()
|
||||
this.resume();
|
||||
} catch (error) {
|
||||
console.log("failed to start WebsocketProvider", error);
|
||||
// @TODO: now what? Attempt reconnect?
|
||||
@ -44,6 +54,21 @@ 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();
|
||||
}
|
||||
};
|
||||
*/
|
||||
}
|
||||
|
||||
async _write(message: string): Promise<void> {
|
||||
|
Loading…
Reference in New Issue
Block a user