feat: add reconnection strategy
This commit is contained in:
parent
6c6e209836
commit
c21b623ffd
1 changed files with 32 additions and 11 deletions
43
index.ts
43
index.ts
|
@ -1,26 +1,47 @@
|
|||
import { Logger } from "@utils/Logger";
|
||||
import definePlugin from "@utils/types";
|
||||
import { FluxDispatcher } from "@webpack/common";
|
||||
|
||||
const logger = new Logger("muter");
|
||||
const reconnect_interval = 5000;
|
||||
const port = 3034;
|
||||
let muted = false;
|
||||
let socket: WebSocket;
|
||||
|
||||
function connect() {
|
||||
logger.info(`Attempting to connect to muter on port ${port}`);
|
||||
socket = new WebSocket(`ws://127.0.0.1:${port}`);
|
||||
|
||||
socket.onopen = () => {
|
||||
logger.info("Connected to muter");
|
||||
};
|
||||
|
||||
socket.onmessage = (event) => {
|
||||
muted = event.data.size > 0;
|
||||
FluxDispatcher.dispatch({
|
||||
type: "AUDIO_SET_SELF_MUTE",
|
||||
context: "default",
|
||||
mute: muted
|
||||
});
|
||||
};
|
||||
|
||||
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() {
|
||||
socket = new WebSocket("ws://localhost:3034");
|
||||
|
||||
socket.onmessage = (event) => {
|
||||
muted = event.data.size > 0;
|
||||
FluxDispatcher.dispatch({
|
||||
type: "AUDIO_SET_SELF_MUTE",
|
||||
context: "default",
|
||||
mute: muted
|
||||
});
|
||||
};
|
||||
|
||||
connect();
|
||||
// todo: try to fix weird virtual bug
|
||||
},
|
||||
flux: {
|
||||
|
|
Loading…
Reference in a new issue