https://github.com/matthieugrieger/mumbledj/issues/40: Add auto-reconnect on disconnect
This commit is contained in:
parent
f80ffbc745
commit
d43dde1943
|
@ -1,6 +1,10 @@
|
|||
MumbleDJ Changelog
|
||||
==================
|
||||
|
||||
### February 25, 2015 -- `v2.6.5`
|
||||
* Added automatic connection retries if the bot loses connection to the server. The bot will attempt to reconnect to the server every 30 seconds for a period of 15 seconds, then exit if
|
||||
a connection cannot be made.
|
||||
|
||||
### February 20, 2015 -- `v2.6.4`
|
||||
* Fixed failed audio downloads for YouTube videos with IDs beginning with "-".
|
||||
|
||||
|
|
22
main.go
22
main.go
|
@ -17,6 +17,7 @@ import (
|
|||
"github.com/layeh/gumble/gumbleutil"
|
||||
"os"
|
||||
"os/user"
|
||||
"time"
|
||||
)
|
||||
|
||||
// MumbleDJ type declaration
|
||||
|
@ -68,7 +69,26 @@ func (dj *mumbledj) OnConnect(e *gumble.ConnectEvent) {
|
|||
|
||||
// OnDisconnect event. Terminates MumbleDJ thread.
|
||||
func (dj *mumbledj) OnDisconnect(e *gumble.DisconnectEvent) {
|
||||
dj.keepAlive <- true
|
||||
if e.Type == gumble.DisconnectError || e.Type == gumble.DisconnectKicked {
|
||||
fmt.Println("Disconnected from server... Will retry connection in 30 second intervals for 15 minutes.")
|
||||
reconnectSuccess := false
|
||||
for retries := 0; retries <= 30; retries++ {
|
||||
fmt.Println("Retrying connection...")
|
||||
if err := dj.client.Connect(); err == nil {
|
||||
fmt.Println("Successfully reconnected to the server!")
|
||||
reconnectSuccess = true
|
||||
break
|
||||
}
|
||||
time.Sleep(30 * time.Second)
|
||||
}
|
||||
if !reconnectSuccess {
|
||||
fmt.Println("Could not reconnect to server. Exiting...")
|
||||
dj.keepAlive <- true
|
||||
os.Exit(1)
|
||||
}
|
||||
} else {
|
||||
dj.keepAlive <- true
|
||||
}
|
||||
}
|
||||
|
||||
// OnTextMessage event. Checks for command prefix, and calls parseCommand if it exists. Ignores
|
||||
|
|
Reference in a new issue