Set a reasonable value for the read deadline

This commit is contained in:
Tyler 2017-05-04 00:31:26 -04:00
parent 91cd88b101
commit 17704469cc
1 changed files with 4 additions and 2 deletions

View File

@ -60,6 +60,7 @@ type TwitchPubSub struct {
responseCh chan *twitchMessage responseCh chan *twitchMessage
SubscribedTopics []string SubscribedTopics []string
LastPing time.Time
} }
func NewTwitchPubSub() *TwitchPubSub { func NewTwitchPubSub() *TwitchPubSub {
@ -91,8 +92,6 @@ func (t *TwitchPubSub) Open() error {
return err return err
} }
c.SetReadDeadline(45 * time.Second)
t.wsConn = c t.wsConn = c
t.listening = make(chan interface{}) 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{}) { func (t *TwitchPubSub) reader(wsConn *websocket.Conn, listening <-chan interface{}) {
for { for {
t.wsConn.SetReadDeadline(t.LastPing.Add(45 * time.Second))
var message twitchMessage var message twitchMessage
err := t.wsConn.ReadJSON(&message) err := t.wsConn.ReadJSON(&message)
@ -299,6 +300,7 @@ 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.wsMutex.Lock() t.wsMutex.Lock()
err := wsConn.WriteJSON(&twitchMessage{Type: Ping}) err := wsConn.WriteJSON(&twitchMessage{Type: Ping})
t.wsMutex.Unlock() t.wsMutex.Unlock()