improved ts3-get

This commit is contained in:
2021-04-22 19:26:45 +02:00
parent 7a668cbd80
commit 2fdcf5935f

View File

@@ -14,6 +14,7 @@ const nodeInit: NodeInitializer = (RED): void => {
let client: TeamSpeak | null = null
let connectPromise: Promise<TeamSpeak> | null = null
let shouldDissconect = false
this.getConnection = async (): Promise<TeamSpeak>=>{
if (client){
@@ -21,6 +22,7 @@ const nodeInit: NodeInitializer = (RED): void => {
}else if (connectPromise){
return connectPromise
}else{
try{
connectPromise = TeamSpeak.connect({
host: host,
protocol: QueryProtocol.RAW,
@@ -30,10 +32,33 @@ const nodeInit: NodeInitializer = (RED): void => {
password: this.credentials.password,
nickname: nickname
})
this.emit("connected")
return await connectPromise
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,{