Optimize StatusLoop. Execute function when specified in OnWsConnect

This commit is contained in:
Wim 2017-07-01 23:28:16 +02:00
parent ba35212b67
commit a58a3e5000
1 changed files with 21 additions and 5 deletions

View File

@ -65,6 +65,7 @@ type MMClient struct {
WsSequence int64
WsPingChan chan *model.WebSocketResponse
ServerVersion string
OnWsConnect func()
}
func New(login, pass, team, server string) *MMClient {
@ -724,6 +725,12 @@ func (m *MMClient) GetTeamId() string {
}
func (m *MMClient) StatusLoop() {
retries := 0
backoff := time.Second * 60
if m.OnWsConnect != nil {
m.OnWsConnect()
}
m.log.Debug("StatusLoop:", m.OnWsConnect)
for {
if m.WsQuit {
return
@ -734,14 +741,23 @@ func (m *MMClient) StatusLoop() {
select {
case <-m.WsPingChan:
m.log.Debug("WS PONG received")
backoff = time.Second * 60
case <-time.After(time.Second * 5):
m.Logout()
m.WsQuit = false
m.Login()
go m.WsReceiver()
if retries > 3 {
m.Logout()
m.WsQuit = false
m.Login()
if m.OnWsConnect != nil {
m.OnWsConnect()
}
go m.WsReceiver()
} else {
retries++
backoff = time.Second * 5
}
}
}
time.Sleep(time.Second * 60)
time.Sleep(backoff)
}
}