improved ts3-get
This commit is contained in:
@@ -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,{
|
||||
|
||||
Reference in New Issue
Block a user