This commit is contained in:
2023-06-26 21:51:50 +08:00
parent 4a9687a045
commit ddf3a0a878
11 changed files with 157 additions and 14 deletions

25
server/cfg.ts Normal file
View File

@@ -0,0 +1,25 @@
import { signal } from "@preact/signals";
import { ConfigType } from "../config.ts";
import { get_ws_host } from "./utils.ts";
import { ConfigClientSocketData, ConfigSeverSocketData } from "./config.ts";
export const cfg = signal<ConfigType | undefined>(undefined);
export function initCfg() {
const ws = new WebSocket(`${get_ws_host()}/api/config?current=1&type=ws`);
console.log(ws);
function sendMessage(mes: ConfigClientSocketData) {
ws.send(JSON.stringify(mes));
}
ws.onmessage = (e) => {
const d: ConfigSeverSocketData = JSON.parse(e.data);
if (d.type === "close") {
ws.close();
} else if (d.type === "cfg") {
cfg.value = d.cfg;
}
};
self.addEventListener("beforeunload", () => {
sendMessage({ type: "close" });
});
}

7
server/config.ts Normal file
View File

@@ -0,0 +1,7 @@
import { ConfigType } from "../config.ts";
export type ConfigClientSocketData = { type: "close" };
export type ConfigSeverSocketData = { type: "close" } | {
type: "cfg";
cfg: ConfigType;
};