From 3b8d0186448b4d2233273db6866c3135bd6a4143 Mon Sep 17 00:00:00 2001 From: Tyler Date: Wed, 10 Oct 2018 00:35:51 -0400 Subject: [PATCH] Add fatal errors to reconnect events to debug --- pubsub.go | 26 ++++++++++++++++++++------ 1 file changed, 20 insertions(+), 6 deletions(-) diff --git a/pubsub.go b/pubsub.go index 134c68f..9929e95 100644 --- a/pubsub.go +++ b/pubsub.go @@ -61,6 +61,7 @@ type TwitchPubSub struct { SubscribedTopics []string LastPing time.Time + LastPong time.Time } func NewTwitchPubSub() *TwitchPubSub { @@ -256,7 +257,11 @@ func (t *TwitchPubSub) reader(wsConn *websocket.Conn, listening <-chan interface if sameConnection { t.Close() - t.reconnect() + err := t.reconnect() + + if err != nil { + log.Fatalln("Unable to reconnect:", err) + } } return } @@ -267,6 +272,7 @@ func (t *TwitchPubSub) reader(wsConn *websocket.Conn, listening <-chan interface default: if message.Type == Pong { // PONG! + t.LastPong = time.Now() } else if message.Type == Message { var data interface{} @@ -278,14 +284,14 @@ 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) - server_time := time.Unix(int64(m["server_time"].(float64)), 0) + serverTime := time.Unix(int64(m["serverTime"].(float64)), 0) if ty == "viewcount" { - go t.handle(ty, &ViewerCount{Channel: ch, Viewers: int(m["viewers"].(float64)), ServerTime: server_time}) + go t.handle(ty, &ViewerCount{Channel: ch, Viewers: int(m["viewers"].(float64)), ServerTime: serverTime}) } else if ty == "stream-up" { - go t.handle(ty, &StreamUp{Channel: ch, ServerTime: server_time}) + go t.handle(ty, &StreamUp{Channel: ch, ServerTime: serverTime}) } else if ty == "stream-down" { - go t.handle(ty, &StreamDown{Channel: ch, ServerTime: server_time}) + go t.handle(ty, &StreamDown{Channel: ch, ServerTime: serverTime}) } } } else if message.Type == Response { @@ -293,7 +299,11 @@ func (t *TwitchPubSub) reader(wsConn *websocket.Conn, listening <-chan interface } else if message.Type == reconnectEventType { go t.handle(message.Type, &Reconnect{}) t.Close() - t.reconnect() + err := t.reconnect() + + if err != nil { + log.Fatalln("Unable to reconnect after event:", err) + } } } } @@ -307,12 +317,16 @@ func (t *TwitchPubSub) pinger(wsConn *websocket.Conn, listening <-chan interface if t.debug { log.Println("Sending ping") } + t.LastPing = time.Now() t.wsMutex.Lock() err := wsConn.WriteJSON(&twitchMessage{Type: Ping}) t.wsMutex.Unlock() if err != nil { + if t.debug { + log.Println("Unable to send ping:", err) + } return }