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 client: TeamSpeak | null = null
let connectPromise: Promise<TeamSpeak> | null = null let connectPromise: Promise<TeamSpeak> | null = null
let shouldDissconect = false
this.getConnection = async (): Promise<TeamSpeak>=>{ this.getConnection = async (): Promise<TeamSpeak>=>{
if (client){ if (client){
@@ -21,19 +22,43 @@ const nodeInit: NodeInitializer = (RED): void => {
}else if (connectPromise){ }else if (connectPromise){
return connectPromise return connectPromise
}else{ }else{
connectPromise = TeamSpeak.connect({ try{
host: host, connectPromise = TeamSpeak.connect({
protocol: QueryProtocol.RAW, host: host,
queryport: 10011, protocol: QueryProtocol.RAW,
serverport: 9987, queryport: 10011,
username: this.credentials.username, serverport: 9987,
password: this.credentials.password, username: this.credentials.username,
nickname: nickname 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,{ RED.nodes.registerType("ts3-config", Ts3Config,{