improved ts3-get
This commit is contained in:
@@ -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,6 +22,7 @@ const nodeInit: NodeInitializer = (RED): void => {
|
|||||||
}else if (connectPromise){
|
}else if (connectPromise){
|
||||||
return connectPromise
|
return connectPromise
|
||||||
}else{
|
}else{
|
||||||
|
try{
|
||||||
connectPromise = TeamSpeak.connect({
|
connectPromise = TeamSpeak.connect({
|
||||||
host: host,
|
host: host,
|
||||||
protocol: QueryProtocol.RAW,
|
protocol: QueryProtocol.RAW,
|
||||||
@@ -30,10 +32,33 @@ const nodeInit: NodeInitializer = (RED): void => {
|
|||||||
password: this.credentials.password,
|
password: this.credentials.password,
|
||||||
nickname: nickname
|
nickname: nickname
|
||||||
})
|
})
|
||||||
this.emit("connected")
|
client = await connectPromise
|
||||||
return 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,{
|
||||||
|
|||||||
Reference in New Issue
Block a user