From 17704469ccd6c80e061b3be2067819c72fb7774d Mon Sep 17 00:00:00 2001 From: Tyler Date: Thu, 4 May 2017 00:31:26 -0400 Subject: [PATCH] Set a reasonable value for the read deadline --- pubsub.go | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) 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()