Ensure we ping before sending anything

This commit is contained in:
Tyler 2018-10-10 05:12:16 -04:00
parent 3b8d018644
commit 48f0722cd7
1 changed files with 15 additions and 6 deletions

View File

@ -101,13 +101,17 @@ func (t *TwitchPubSub) Open() error {
go t.reader(t.wsConn, t.listening)
go t.pinger(t.wsConn, t.listening)
t.wsMutex.Lock()
t.LastPing = time.Now()
err = t.wsConn.WriteJSON(&twitchMessage{Type: Ping})
t.wsMutex.Unlock()
if err != nil {
return err
}
if len(t.SubscribedTopics) > 0 {
return t.listen(t.SubscribedTopics)
} else {
t.wsMutex.Lock()
defer t.wsMutex.Unlock()
t.LastPing = time.Now()
return t.wsConn.WriteJSON(&twitchMessage{Type: Ping})
}
return nil
@ -284,7 +288,12 @@ func (t *TwitchPubSub) reader(wsConn *websocket.Conn, listening <-chan interface
m := data.(map[string]interface{})
ch := message.Data.Topic[strings.Index(message.Data.Topic, ".") + 1:]
ty := m["type"].(string)
serverTime := time.Unix(int64(m["serverTime"].(float64)), 0)
serverTime := time.Now()
if v, ok := m["serverTime"]; ok {
serverTime = time.Unix(int64(v.(float64)), 0)
}
if ty == "viewcount" {
go t.handle(ty, &ViewerCount{Channel: ch, Viewers: int(m["viewers"].(float64)), ServerTime: serverTime})