Add fatal errors to reconnect events to debug

This commit is contained in:
Tyler 2018-10-10 00:35:51 -04:00
parent 6223650a8c
commit 3b8d018644
1 changed files with 20 additions and 6 deletions

View File

@ -61,6 +61,7 @@ type TwitchPubSub struct {
SubscribedTopics []string SubscribedTopics []string
LastPing time.Time LastPing time.Time
LastPong time.Time
} }
func NewTwitchPubSub() *TwitchPubSub { func NewTwitchPubSub() *TwitchPubSub {
@ -256,7 +257,11 @@ func (t *TwitchPubSub) reader(wsConn *websocket.Conn, listening <-chan interface
if sameConnection { if sameConnection {
t.Close() t.Close()
t.reconnect() err := t.reconnect()
if err != nil {
log.Fatalln("Unable to reconnect:", err)
}
} }
return return
} }
@ -267,6 +272,7 @@ func (t *TwitchPubSub) reader(wsConn *websocket.Conn, listening <-chan interface
default: default:
if message.Type == Pong { if message.Type == Pong {
// PONG! // PONG!
t.LastPong = time.Now()
} else if message.Type == Message { } else if message.Type == Message {
var data interface{} var data interface{}
@ -278,14 +284,14 @@ 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)
server_time := time.Unix(int64(m["server_time"].(float64)), 0) serverTime := time.Unix(int64(m["serverTime"].(float64)), 0)
if ty == "viewcount" { 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" { } 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" { } 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 { } else if message.Type == Response {
@ -293,7 +299,11 @@ func (t *TwitchPubSub) reader(wsConn *websocket.Conn, listening <-chan interface
} else if message.Type == reconnectEventType { } else if message.Type == reconnectEventType {
go t.handle(message.Type, &Reconnect{}) go t.handle(message.Type, &Reconnect{})
t.Close() 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 { if t.debug {
log.Println("Sending ping") log.Println("Sending ping")
} }
t.LastPing = time.Now() t.LastPing = time.Now()
t.wsMutex.Lock() t.wsMutex.Lock()
err := wsConn.WriteJSON(&twitchMessage{Type: Ping}) err := wsConn.WriteJSON(&twitchMessage{Type: Ping})
t.wsMutex.Unlock() t.wsMutex.Unlock()
if err != nil { if err != nil {
if t.debug {
log.Println("Unable to send ping:", err)
}
return return
} }