fix: enable soft reconnect #119

Merged
t.behrendt merged 1 commits from fix-enable-soft-reconnect into main 2026-05-07 19:10:17 +02:00
+23 -4
View File
@@ -1,13 +1,13 @@
import type { Gotify } from "gotify"; import type { Gotify } from "gotify";
import type { Logger } from "pino"; import type { Logger } from "pino";
import type { TeamSpeak, TextMessageTargetMode } from "ts3-nodejs-library"; import type { TeamSpeak, TextMessageTargetMode } from "ts3-nodejs-library";
import type { GotifyConfig, Mode } from "./types";
import { import {
ClientConnect, ClientConnect,
ClientDisconnect, ClientDisconnect,
ClientMoved, ClientMoved,
TextMessage, TextMessage,
} from "ts3-nodejs-library/lib/types/Events"; } from "ts3-nodejs-library/lib/types/Events";
import type { GotifyConfig, Mode } from "./types";
function resolveMessageTarget(target: TextMessageTargetMode): string { function resolveMessageTarget(target: TextMessageTargetMode): string {
if (target === 1) { if (target === 1) {
@@ -45,6 +45,8 @@ export function ts3gotifyFactory(
gotifyConfig: GotifyConfig, gotifyConfig: GotifyConfig,
logger: Logger logger: Logger
) { ) {
let reconnectInProgress = false;
function sendNotification(message: string) { function sendNotification(message: string) {
gotifyClient gotifyClient
.send({ .send({
@@ -87,15 +89,32 @@ export function ts3gotifyFactory(
} }
} }
ts3Client.on("close", async () => { async function reconnectWithBackoff() {
if (reconnectInProgress) {
return;
}
reconnectInProgress = true;
logger.info("disconnected, trying to reconnect..."); logger.info("disconnected, trying to reconnect...");
await ts3Client.reconnect(5, 1000);
try {
await ts3Client.reconnect(10, 2000);
logger.info("reconnected!"); logger.info("reconnected!");
} catch (error) {
const message = error instanceof Error ? error.message : String(error);
logger.error(`Error reconnecting to TS3 server: ${message}`);
} finally {
reconnectInProgress = false;
}
}
ts3Client.on("close", async () => {
await reconnectWithBackoff();
}); });
ts3Client.on("error", (error: Error) => { ts3Client.on("error", (error: Error) => {
logger.error(`Error connecting to TS3 server: ${error.message}`); logger.error(`Error connecting to TS3 server: ${error.message}`);
process.exit(1); reconnectWithBackoff();
}); });
return { return {