Close and then reconnect on reconnect signal

This commit is contained in:
Tyler 2018-10-07 19:30:17 -04:00
parent 7dd180eb83
commit 6223650a8c
1 changed files with 8 additions and 7 deletions

View File

@ -3,13 +3,13 @@ package twitchpubsub
import ( import (
"time" "time"
"github.com/gorilla/websocket"
"github.com/google/uuid"
"strings"
"errors"
"encoding/json" "encoding/json"
"sync" "errors"
"github.com/google/uuid"
"github.com/gorilla/websocket"
"log" "log"
"strings"
"sync"
) )
const ( const (
@ -194,7 +194,7 @@ func (t *TwitchPubSub) listen(topics []string) error {
if msg.Nonce != nonce { if msg.Nonce != nonce {
// Something is wrong, concurrent access? // Something is wrong, concurrent access?
return errors.New("Unexpected nonce") return errors.New("unexpected nonce")
} }
if len(msg.Error) > 0 { if len(msg.Error) > 0 {
@ -228,7 +228,7 @@ func (t *TwitchPubSub) Unlisten(topics []string) error {
if msg.Nonce != nonce { if msg.Nonce != nonce {
// Something is wrong, concurrent access? // Something is wrong, concurrent access?
return errors.New("Unexpected nonce") return errors.New("unexpected nonce")
} }
if len(msg.Error) > 0 { if len(msg.Error) > 0 {
@ -292,6 +292,7 @@ func (t *TwitchPubSub) reader(wsConn *websocket.Conn, listening <-chan interface
t.responseCh <- &message t.responseCh <- &message
} 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.reconnect() t.reconnect()
} }
} }