diff --git a/pubsub.go b/pubsub.go index 9929e95..2f20916 100644 --- a/pubsub.go +++ b/pubsub.go @@ -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})