mirror of
https://github.com/wavetermdev/backup.git
synced 2026-08-05 13:57:07 -07:00
updated ws loading code (#397)
This commit is contained in:
+19
-16
@@ -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 };
|
||||
|
||||
@@ -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(() => {
|
||||
|
||||
Reference in New Issue
Block a user