feat: add reconnection strategy

This commit is contained in:
newt 2024-11-17 12:50:54 +00:00
parent 6c6e209836
commit c21b623ffd

View file

@ -1,16 +1,20 @@
import { Logger } from "@utils/Logger";
import definePlugin from "@utils/types"; import definePlugin from "@utils/types";
import { FluxDispatcher } from "@webpack/common"; import { FluxDispatcher } from "@webpack/common";
const logger = new Logger("muter");
const reconnect_interval = 5000;
const port = 3034;
let muted = false; let muted = false;
let socket: WebSocket; let socket: WebSocket;
export default definePlugin({ function connect() {
name: "muter", logger.info(`Attempting to connect to muter on port ${port}`);
description: "muter companion plugin. Mirrors system mutes to Discord.", socket = new WebSocket(`ws://127.0.0.1:${port}`);
authors: [{ name: "isitreallyalive", id: 1269669249056510026n }],
start() { socket.onopen = () => {
socket = new WebSocket("ws://localhost:3034"); logger.info("Connected to muter");
};
socket.onmessage = (event) => { socket.onmessage = (event) => {
muted = event.data.size > 0; muted = event.data.size > 0;
@ -21,6 +25,23 @@ export default definePlugin({
}); });
}; };
socket.onclose = e => {
if (!e.wasClean) {
logger.error("Connection to muter was interrupted. Reconnecting...");
setTimeout(connect, reconnect_interval);
} else {
logger.info("Connection to muter closed");
}
};
}
export default definePlugin({
name: "muter",
description: "muter companion plugin. Mirrors system mutes to Discord.",
authors: [{ name: "isitreallyalive", id: 1269669249056510026n }],
start() {
connect();
// todo: try to fix weird virtual bug // todo: try to fix weird virtual bug
}, },
flux: { flux: {