Skip to content

Commit

Permalink
implement full reconnect logic
Browse files Browse the repository at this point in the history
  • Loading branch information
Multivit4min committed Nov 24, 2019
1 parent 78147d2 commit 98851de
Show file tree
Hide file tree
Showing 3 changed files with 50 additions and 4 deletions.
44 changes: 44 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -202,6 +202,44 @@ When the Query gets accused of Flooding then it will return error with id 524 an
This will be parsed automatically and the Query will wait for the given time (normally its 1 second) + 100 additional milliseconds (sometimes it happens the query gets banned when still sending too early)


# Reconnecting


With version 2.3 this library is able to reconnect to its TeamSpeak Server when the connection has been lost.
It restores its full context this includes:
* selecting the server
* logging in with the last credentials
* subscribing to all used events
* selecting the correct nickname

all commands which have been added in meantime while the teamspeak server was not connected will be still executed after and all pending commands will be sent AFTER connecting and restoring the context

an example on how this looks like:

```typescript
import { TeamSpeak, QueryProtocol } from "./src/TeamSpeak"

TeamSpeak.connect({
host: "127.0.0.1",
queryport: 10011,
serverport: 9987,
protocol: QueryProtocol.RAW,
username: "serveradmin",
password: "xxx",
nickname: "test"
}).then(async teamspeak => {

teamspeak.on("close", async () => {
console.log("disconnected, trying to reconnect...")
await teamspeak.reconnect(-1, 1000)
console.log("reconnected!")
})

})
```



# Update Notes from 1.x to 2.x

With version 2.x support for Client Events has been dropped instead use the events from the main class TeamSpeak.
Expand All @@ -211,6 +249,12 @@ The testing environment now runs via jest which makes mocking and testing easier
Documentation software has been switched from `documentation` to `typedoc`


# Update Notes to 2.3

The `close` event now only gets fired when a connection has been successfully established first!
In order to get errors when connecting to a server use the `error` event instead. This was required in order to implement the reconnect logic.


# Authors

* **David Kartnaller** ([Multivit4min](https://github.com/Multivit4min)) - *Initial work*
Expand Down
8 changes: 5 additions & 3 deletions src/TeamSpeak.ts
Original file line number Diff line number Diff line change
Expand Up @@ -145,12 +145,12 @@ export class TeamSpeak extends EventEmitter {

/**
* attempts a reconnect to the teamspeak server with full context features
* @param attempts the amount of times it should try to reconnect
* @param attempts the amount of times it should try to reconnect (-1 = try forever)
* @param timeout time in ms to wait inbetween reconnect
*/
async reconnect(attempts: number = 1, timeout: number = 2000) {
let error: Error|null = null
while (attempts-- > 0) {
while (attempts === -1 || attempts-- > 0) {
try {
await TeamSpeak.wait(timeout)
if (this.query.isConnected()) throw new Error("already connected")
Expand Down Expand Up @@ -205,7 +205,9 @@ export class TeamSpeak extends EventEmitter {
/** handles initial commands after successfully connecting to a TeamSpeak Server */
private handleReady() {
const exec: Promise<any>[] = []
if (this.config.username && this.config.password && this.config.protocol === "raw") {
if (this.context.login && this.config.protocol === QueryProtocol.RAW) {
exec.push(this.priorize().login(this.context.login.username, this.context.login.password))
} else if (this.config.username && this.config.password && this.config.protocol === QueryProtocol.RAW) {
exec.push(this.priorize().login(this.config.username, this.config.password))
}
if (this.context.selectType !== SelectType.NONE) {
Expand Down
2 changes: 1 addition & 1 deletion src/transport/TeamSpeakQuery.ts
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,6 @@ export class TeamSpeakQuery extends EventEmitter {
this.socket.on("connect", this.handleConnect.bind(this))
this.socket.on("line", this.handleLine.bind(this))
this.socket.on("error", this.handleError.bind(this))
this.socket.on("close", this.handleClose.bind(this))
}

/** returns a constructed Socket */
Expand Down Expand Up @@ -120,6 +119,7 @@ export class TeamSpeakQuery extends EventEmitter {
/** gets called when the underlying transport layer connects to a server */
private handleConnect() {
this.connected = true
this.socket.on("close", this.handleClose.bind(this))
this.emit("connect")
}

Expand Down

0 comments on commit 98851de

Please sign in to comment.