updated ws loading code (#397)

This commit is contained in:
Mike Sawka
2024-09-19 10:42:53 -07:00
committed by GitHub
parent 0059b61748
commit d2366df9db
5 changed files with 59 additions and 20 deletions
+19 -16
View File
@@ -3,18 +3,10 @@
import debug from "debug";
import { sprintf } from "sprintf-js";
import type { WebSocket as ElectronWebSocketType } from "ws";
import type { WebSocket as NodeWebSocketType } from "ws";
let ElectronWebSocket: typeof ElectronWebSocketType;
const AuthKeyHeader = "X-AuthKey";
if (typeof window === "undefined") {
try {
const WebSocket = require("ws") as typeof ElectronWebSocketType;
ElectronWebSocket = WebSocket;
} catch (e) {}
}
const dlog = debug("wave:ws");
const WarnWebSocketSendSize = 1024 * 1024; // 1MB
@@ -34,8 +26,13 @@ function removeWSReconnectHandler(handler: () => void) {
type WSEventCallback = (arg0: WSEventType) => void;
type ElectronOverrideOpts = {
wsImpl: typeof NodeWebSocketType;
authKey: string;
};
class WSControl {
wsConn: WebSocket | ElectronWebSocketType;
wsConn: WebSocket | NodeWebSocketType;
open: boolean;
opening: boolean = false;
reconnectTimes: number = 0;
@@ -47,14 +44,19 @@ class WSControl {
wsLog: string[] = [];
baseHostPort: string;
lastReconnectTime: number = 0;
authKey: string = null; // used only by electron
eoOpts: ElectronOverrideOpts;
constructor(baseHostPort: string, windowId: string, messageCallback: WSEventCallback, authKey?: string) {
constructor(
baseHostPort: string,
windowId: string,
messageCallback: WSEventCallback,
electronOverrideOpts?: ElectronOverrideOpts
) {
this.baseHostPort = baseHostPort;
this.messageCallback = messageCallback;
this.windowId = windowId;
this.open = false;
this.authKey = authKey;
this.eoOpts = electronOverrideOpts;
setInterval(this.sendPing.bind(this), 5000);
}
@@ -65,9 +67,9 @@ class WSControl {
this.lastReconnectTime = Date.now();
dlog("try reconnect:", desc);
this.opening = true;
if (ElectronWebSocket) {
this.wsConn = new ElectronWebSocket(this.baseHostPort + "/ws?windowid=" + this.windowId, {
headers: { [AuthKeyHeader]: this.authKey },
if (this.eoOpts) {
this.wsConn = new this.eoOpts.wsImpl(this.baseHostPort + "/ws?windowid=" + this.windowId, {
headers: { [AuthKeyHeader]: this.eoOpts.authKey },
});
} else {
this.wsConn = new WebSocket(this.baseHostPort + "/ws?windowid=" + this.windowId);
@@ -208,3 +210,4 @@ class WSControl {
}
export { addWSReconnectHandler, removeWSReconnectHandler, WSControl };
export type { ElectronOverrideOpts };
+3 -3
View File
@@ -5,7 +5,7 @@ import { wpsReconnectHandler } from "@/app/store/wps";
import { WshClient } from "@/app/store/wshclient";
import { makeWindowRouteId, WshRouter } from "@/app/store/wshrouter";
import { getWSServerEndpoint } from "@/util/endpoints";
import { addWSReconnectHandler, WSControl } from "./ws";
import { addWSReconnectHandler, ElectronOverrideOpts, WSControl } from "./ws";
let globalWS: WSControl;
let DefaultRouter: WshRouter;
@@ -114,12 +114,12 @@ if (globalThis.window != null) {
globalThis["consumeGenerator"] = consumeGenerator;
}
function initElectronWshrpc(electronClient: WshClient, authKey: string) {
function initElectronWshrpc(electronClient: WshClient, eoOpts: ElectronOverrideOpts) {
DefaultRouter = new WshRouter(new UpstreamWshRpcProxy());
const handleFn = (event: WSEventType) => {
DefaultRouter.recvRpcMessage(event.data);
};
globalWS = new WSControl(getWSServerEndpoint(), "electron", handleFn, authKey);
globalWS = new WSControl(getWSServerEndpoint(), "electron", handleFn, eoOpts);
globalWS.connectNow("connectWshrpc");
DefaultRouter.registerRoute(electronClient.routeId, electronClient);
addWSReconnectHandler(() => {