[🤯 Refactor] Use standalone window for WebSocket connections

1. [-] 删除了对于 desktopAssistant 的 Hook 逻辑
2. [/] 现在不再使用 desktopAssistant 保活 WS 连接, 而是用一个单独的不可见窗口处理 WS (#25)
This commit is contained in:
Minoricew
2025-06-17 18:14:58 +08:00
parent f6a30351fd
commit 807ac913da
8 changed files with 84 additions and 23 deletions

View File

@@ -418,7 +418,7 @@ const applyPlsIpcHandler = (ipcMain) => {
*/ */
(_event, arg) => { (_event, arg) => {
ipcMain.send( ipcMain.send(
"desktopAssistant", "auraWsKeepAlive",
`${methodBase}.ws.post.onReqSendMsg`, `${methodBase}.ws.post.onReqSendMsg`,
arg arg
); );
@@ -568,7 +568,7 @@ const applyPlsIpcHandler = (ipcMain) => {
status: "Already", status: "Already",
}; };
} else { } else {
ipcMain.send("desktopAssistant", `${methodBase}.retryPlsConnect`, arg); ipcMain.send("auraWsKeepAlive", `${methodBase}.retryPlsConnect`, arg);
return { return {
success: true, success: true,

View File

@@ -14,7 +14,7 @@ class WindowHooksManager {
const hooksPath = path.join(__dirname, "../../../aura/mainProcess/hooks"); const hooksPath = path.join(__dirname, "../../../aura/mainProcess/hooks");
/** @type {import("../../types/main/core").HooksMap} */ /** @type {import("../../types/main/core").UIHooksMap} */
const hooks = new Map(); const hooks = new Map();
try { try {
@@ -55,6 +55,9 @@ class WindowHooksManager {
browserWindowInstance browserWindowInstance
) { ) {
const stripWindowName = windowName.split("_")[0]; const stripWindowName = windowName.split("_")[0];
if (!global.__HUGO_AURA__.windowHooks) return;
if (!global.__HUGO_AURA__.windowHooks.has(stripWindowName)) { if (!global.__HUGO_AURA__.windowHooks.has(stripWindowName)) {
console.log( console.log(
`[HugoAura / Init / WDH] Window ${windowName} has no corresponding main process hooks, ignoring...` `[HugoAura / Init / WDH] Window ${windowName} has no corresponding main process hooks, ignoring...`

View File

@@ -8,7 +8,7 @@ interface AssistantHugoAuraGlobal extends HugoAuraGlobal {
plsSettings: Record<any, any>; plsSettings: Record<any, any>;
} }
interface DesktopAssistantHugoAuraGlobal extends HugoAuraGlobal { interface AuraWSKeepAliveWindowHugoAuraGlobal extends HugoAuraGlobal {
plsWs: WebSocket | null; plsWs: WebSocket | null;
plsStats: PLSStatus; plsStats: PLSStatus;
} }

View File

@@ -1,15 +0,0 @@
// @ts-check
/**
* @type {import("../../types/render/uiHook").UIHookConfig}
*/
const def = {
targets: {},
globalStyles: ["ui/css/global.css"],
globalJS: ["ui/js/global.js", "ui/js/plsConnectionManager.js"],
onLoaded: `
console.log('[HugoAura / UI / Hooks / Desktop Assistant] Page loaded.');
`,
};
module.exports = def;

View File

@@ -1,5 +1,12 @@
// @ts-check // @ts-check
(() => { (() => {
if (!global.__HUGO_AURA__)
global.__HUGO_AURA__ = {
configInit: true,
auraDir: "",
version: "",
};
if (!global.__HUGO_AURA__.plsStats) if (!global.__HUGO_AURA__.plsStats)
global.__HUGO_AURA__.plsStats = { global.__HUGO_AURA__.plsStats = {
installed: false, installed: false,
@@ -12,8 +19,8 @@
}; };
const IPC_METHOD_BASE = "$aura.pls"; const IPC_METHOD_BASE = "$aura.pls";
const REQUIRE_BASE = "../../aura/ui"; const REQUIRE_BASE = "../../..";
const __SCOPE = "desktopAssistant"; const __SCOPE = "auraWsKeepAlive";
const PLS_REG_PATH = "ProxyLayerServices"; const PLS_REG_PATH = "ProxyLayerServices";
@@ -312,7 +319,7 @@
const onSetup = () => { const onSetup = () => {
if (!global.ipcRenderer) { if (!global.ipcRenderer) {
// @ts-ignore // @ts-ignore
global.ipcRenderer = require("electron").global.ipcRenderer; global.ipcRenderer = require("electron").ipcRenderer;
} }
initPlsConnection(); initPlsConnection();

View File

@@ -0,0 +1,16 @@
<!DOCTYPE html>
<html>
<head>
<title>Aura WebSocket KeepAlive Window</title>
<style>
:root {
opacity: 0;
}
</style>
<script>
let global = window;
</script>
<script src="../../../js/plsConnectionManager.js"></script>
</head>
<body></body>
</html>

39
src/aura/utils/pls.js Normal file
View File

@@ -0,0 +1,39 @@
// @ts-check
/**
* @param {Electron} electron
*/
const createWsWindow = (electron) => {
const path = require("path");
const { BrowserWindow } = electron;
const window = new BrowserWindow({
width: 0,
height: 0,
frame: false,
skipTaskbar: true,
transparent: true,
alwaysOnTop: true,
webPreferences: {
nodeIntegration: true,
contextIsolation: false,
devTools: true,
},
});
window.setIgnoreMouseEvents(true);
window.loadFile(
path.join(
__dirname,
"..",
"ui",
"pages",
"windows",
"auraWsKeepAlive",
"index.html"
)
);
return window;
};
module.exports = { createWsWindow };

View File

@@ -47,6 +47,7 @@ const NetworkHook = require("../aura/init/rendererHook/networkHook");
const ConfigManager = require("../aura/init/shared/configManager"); const ConfigManager = require("../aura/init/shared/configManager");
const RegistryManager = require("../aura/init/shared/registryManager"); const RegistryManager = require("../aura/init/shared/registryManager");
const { buildIpcMain } = require("../aura/init/main/ipcHandler"); const { buildIpcMain } = require("../aura/init/main/ipcHandler");
const plsUtils = require("../aura/utils/pls");
const { initLogger } = require("../aura/init/main/logger"); const { initLogger } = require("../aura/init/main/logger");
@@ -97,7 +98,10 @@ const launcher = ({ central, windowName, config }) => {
app.exit(0); app.exit(0);
}; };
global.__HUGO_AURA__.auraDir = path.join(getUserDocumentsDirPath(), "HugoAura"); global.__HUGO_AURA__.auraDir = path.join(
getUserDocumentsDirPath(),
"HugoAura"
);
// >>> Init Logger <<< // // >>> Init Logger <<< //
initLogger(windowName); initLogger(windowName);
@@ -146,6 +150,13 @@ const launcher = ({ central, windowName, config }) => {
config.canOpenDevTool = true; config.canOpenDevTool = true;
} }
// >>> Create WebSocket KeepAlive Window <<< //
if (!global.__HUGO_AURA__.hookedWindows?.has("auraWsKeepAlive")) {
const wsKaWin = plsUtils.createWsWindow(electron);
// @ts-expect-error
global.__HUGO_AURA__.hookedWindows.set("auraWsKeepAlive", wsKaWin);
}
// >>> Listeners <<< // // >>> Listeners <<< //
/** /**