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,{