diff --git a/src/ts3gotify.ts b/src/ts3gotify.ts index 0426a80..babd587 100644 --- a/src/ts3gotify.ts +++ b/src/ts3gotify.ts @@ -1,13 +1,13 @@ import type { Gotify } from "gotify"; import type { Logger } from "pino"; import type { TeamSpeak, TextMessageTargetMode } from "ts3-nodejs-library"; -import type { GotifyConfig, Mode } from "./types"; import { ClientConnect, ClientDisconnect, ClientMoved, TextMessage, } from "ts3-nodejs-library/lib/types/Events"; +import type { GotifyConfig, Mode } from "./types"; function resolveMessageTarget(target: TextMessageTargetMode): string { if (target === 1) { @@ -45,6 +45,8 @@ export function ts3gotifyFactory( gotifyConfig: GotifyConfig, logger: Logger ) { + let reconnectInProgress = false; + function sendNotification(message: string) { gotifyClient .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..."); - await ts3Client.reconnect(5, 1000); - logger.info("reconnected!"); + + try { + await ts3Client.reconnect(10, 2000); + 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) => { logger.error(`Error connecting to TS3 server: ${error.message}`); - process.exit(1); + reconnectWithBackoff(); }); return {