Ensure we ping before sending anything
This commit is contained in:
parent
3b8d018644
commit
48f0722cd7
21
pubsub.go
21
pubsub.go
|
@ -101,13 +101,17 @@ func (t *TwitchPubSub) Open() error {
|
||||||
go t.reader(t.wsConn, t.listening)
|
go t.reader(t.wsConn, t.listening)
|
||||||
go t.pinger(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 {
|
if len(t.SubscribedTopics) > 0 {
|
||||||
return t.listen(t.SubscribedTopics)
|
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
|
return nil
|
||||||
|
@ -284,7 +288,12 @@ func (t *TwitchPubSub) reader(wsConn *websocket.Conn, listening <-chan interface
|
||||||
m := data.(map[string]interface{})
|
m := data.(map[string]interface{})
|
||||||
ch := message.Data.Topic[strings.Index(message.Data.Topic, ".") + 1:]
|
ch := message.Data.Topic[strings.Index(message.Data.Topic, ".") + 1:]
|
||||||
ty := m["type"].(string)
|
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" {
|
if ty == "viewcount" {
|
||||||
go t.handle(ty, &ViewerCount{Channel: ch, Viewers: int(m["viewers"].(float64)), ServerTime: serverTime})
|
go t.handle(ty, &ViewerCount{Channel: ch, Viewers: int(m["viewers"].(float64)), ServerTime: serverTime})
|
||||||
|
|
Loading…
Reference in New Issue