2022-09-05 16:57:11 -04:00
|
|
|
"use strict";
|
|
|
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
|
|
exports.WebSocketProvider = void 0;
|
|
|
|
const ws_js_1 = require("./ws.js"); /*-browser*/
|
|
|
|
const provider_socket_js_1 = require("./provider-socket.js");
|
|
|
|
class WebSocketProvider extends provider_socket_js_1.SocketProvider {
|
2022-10-01 01:34:06 -04:00
|
|
|
#connect;
|
2022-09-05 16:57:11 -04:00
|
|
|
#websocket;
|
2022-09-29 22:57:27 -04:00
|
|
|
get websocket() {
|
|
|
|
if (this.#websocket == null) {
|
|
|
|
throw new Error("websocket closed");
|
|
|
|
}
|
|
|
|
return this.#websocket;
|
|
|
|
}
|
2022-09-05 16:57:11 -04:00
|
|
|
constructor(url, network) {
|
|
|
|
super(network);
|
|
|
|
if (typeof (url) === "string") {
|
2022-10-01 01:34:06 -04:00
|
|
|
this.#connect = () => { return new ws_js_1.WebSocket(url); };
|
|
|
|
this.#websocket = this.#connect();
|
|
|
|
}
|
|
|
|
else if (typeof (url) === "function") {
|
|
|
|
this.#connect = url;
|
|
|
|
this.#websocket = url();
|
2022-09-05 16:57:11 -04:00
|
|
|
}
|
|
|
|
else {
|
2022-10-01 01:34:06 -04:00
|
|
|
this.#connect = null;
|
2022-09-05 16:57:11 -04:00
|
|
|
this.#websocket = url;
|
|
|
|
}
|
2022-09-27 03:45:27 -04:00
|
|
|
this.websocket.onopen = async () => {
|
|
|
|
try {
|
|
|
|
await this._start();
|
2022-10-01 01:34:06 -04:00
|
|
|
this.resume();
|
2022-09-27 03:45:27 -04:00
|
|
|
}
|
|
|
|
catch (error) {
|
|
|
|
console.log("failed to start WebsocketProvider", error);
|
|
|
|
// @TODO: now what? Attempt reconnect?
|
|
|
|
}
|
2022-09-05 16:57:11 -04:00
|
|
|
};
|
|
|
|
this.websocket.onmessage = (message) => {
|
|
|
|
this._processMessage(message.data);
|
|
|
|
};
|
2022-10-01 01:34:06 -04:00
|
|
|
/*
|
|
|
|
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();
|
|
|
|
}
|
|
|
|
};
|
|
|
|
*/
|
2022-09-05 16:57:11 -04:00
|
|
|
}
|
|
|
|
async _write(message) {
|
|
|
|
this.websocket.send(message);
|
|
|
|
}
|
2022-09-29 22:57:27 -04:00
|
|
|
async destroy() {
|
2022-12-30 16:35:04 -05:00
|
|
|
if (this.#websocket != null) {
|
|
|
|
this.#websocket.close();
|
|
|
|
this.#websocket = null;
|
2022-09-29 22:57:27 -04:00
|
|
|
}
|
2022-12-30 16:35:04 -05:00
|
|
|
super.destroy();
|
2022-09-29 22:57:27 -04:00
|
|
|
}
|
2022-09-05 16:57:11 -04:00
|
|
|
}
|
|
|
|
exports.WebSocketProvider = WebSocketProvider;
|
|
|
|
//# sourceMappingURL=provider-websocket.js.map
|