diff --git a/pubsub.go b/pubsub.go index 626a612..8f59b53 100644 --- a/pubsub.go +++ b/pubsub.go @@ -60,6 +60,7 @@ type TwitchPubSub struct { responseCh chan *twitchMessage SubscribedTopics []string + LastPing time.Time } func NewTwitchPubSub() *TwitchPubSub { @@ -91,8 +92,6 @@ func (t *TwitchPubSub) Open() error { return err } - c.SetReadDeadline(45 * time.Second) - t.wsConn = c t.listening = make(chan interface{}) @@ -235,6 +234,8 @@ func (t *TwitchPubSub) Unlisten(topics []string) error { func (t *TwitchPubSub) reader(wsConn *websocket.Conn, listening <-chan interface{}) { for { + t.wsConn.SetReadDeadline(t.LastPing.Add(45 * time.Second)) + var message twitchMessage err := t.wsConn.ReadJSON(&message) @@ -299,6 +300,7 @@ 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()