From 2fdcf5935f60addffadfce5a886705b9c3b7b9f0 Mon Sep 17 00:00:00 2001 From: Niklas Date: Thu, 22 Apr 2021 19:26:45 +0200 Subject: [PATCH] improved ts3-get --- src/nodes/ts3-config/ts3-config.ts | 47 +++++++++++++++++++++++------- 1 file changed, 36 insertions(+), 11 deletions(-) diff --git a/src/nodes/ts3-config/ts3-config.ts b/src/nodes/ts3-config/ts3-config.ts index 3237781..a426f73 100644 --- a/src/nodes/ts3-config/ts3-config.ts +++ b/src/nodes/ts3-config/ts3-config.ts @@ -14,6 +14,7 @@ const nodeInit: NodeInitializer = (RED): void => { let client: TeamSpeak | null = null let connectPromise: Promise | null = null + let shouldDissconect = false this.getConnection = async (): Promise=>{ if (client){ @@ -21,19 +22,43 @@ const nodeInit: NodeInitializer = (RED): void => { }else if (connectPromise){ return connectPromise }else{ - connectPromise = TeamSpeak.connect({ - host: host, - protocol: QueryProtocol.RAW, - queryport: 10011, - serverport: 9987, - username: this.credentials.username, - password: this.credentials.password, - nickname: nickname - }) - this.emit("connected") - return await connectPromise + try{ + connectPromise = TeamSpeak.connect({ + host: host, + protocol: QueryProtocol.RAW, + queryport: 10011, + serverport: 9987, + username: this.credentials.username, + password: this.credentials.password, + nickname: nickname + }) + client = await connectPromise + if (client == null){ + throw new Error("Failed to connect to Teamspeak") + } + + client.on("close", async () => { + if (!shouldDissconect){ + if (client != null){ + await client.reconnect(-1, 1000) + }else{ + // TODO: Emit error + } + } + }) + + return client + }catch(err){ + // TODO: handle error + throw err + } } } + + this.on("close",async ()=>{ + shouldDissconect = true + await client?.quit() + }) } RED.nodes.registerType("ts3-config", Ts3Config,{