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
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()