Cleanup, Logging, Finished Resumed code.

This commit is contained in:
Bruce Marriner 2016-04-30 21:40:13 -05:00
parent c65d5091ab
commit a9da8a5daf
4 changed files with 32 additions and 6 deletions

View file

@ -229,6 +229,7 @@ func (s *Session) initialize() {
s.handlersMu.Unlock() s.handlersMu.Unlock()
s.AddHandler(s.onReady) s.AddHandler(s.onReady)
s.AddHandler(s.onResumed)
s.AddHandler(s.onVoiceServerUpdate) s.AddHandler(s.onVoiceServerUpdate)
s.AddHandler(s.onVoiceStateUpdate) s.AddHandler(s.onVoiceStateUpdate)
s.AddHandler(s.State.onInterface) s.AddHandler(s.State.onInterface)
@ -243,3 +244,10 @@ func (s *Session) onReady(se *Session, r *Ready) {
// Start the heartbeat to keep the connection alive. // Start the heartbeat to keep the connection alive.
go s.heartbeat(s.wsConn, s.listening, r.HeartbeatInterval) go s.heartbeat(s.wsConn, s.listening, r.HeartbeatInterval)
} }
// onResumed handles the resumed event.
func (s *Session) onResumed(se *Session, r *Resumed) {
// Start the heartbeat to keep the connection alive.
go s.heartbeat(s.wsConn, s.listening, r.HeartbeatInterval)
}

View file

@ -42,6 +42,7 @@ var eventToInterface = map[string]interface{}{
"TYPING_START": TypingStart{}, "TYPING_START": TypingStart{},
"VOICE_SERVER_UPDATE": VoiceServerUpdate{}, "VOICE_SERVER_UPDATE": VoiceServerUpdate{},
"VOICE_STATE_UPDATE": VoiceStateUpdate{}, "VOICE_STATE_UPDATE": VoiceStateUpdate{},
"RESUMED": Resumed{},
} }
// Connect is an empty struct for an event. // Connect is an empty struct for an event.

View file

@ -100,6 +100,12 @@ type rateLimitMutex struct {
bucket map[string]*sync.Mutex // TODO :) bucket map[string]*sync.Mutex // TODO :)
} }
// A Resumed struct holds the data received in a RESUMED event
type Resumed struct {
HeartbeatInterval time.Duration `json:"heartbeat_interval"`
Trace []string `json:"_trace"`
}
// A VoiceRegion stores data for a specific voice region server. // A VoiceRegion stores data for a specific voice region server.
type VoiceRegion struct { type VoiceRegion struct {
ID string `json:"id"` ID string `json:"id"`

View file

@ -111,8 +111,6 @@ func (s *Session) Open() (err error) {
p.Data.Sequence = s.sequence p.Data.Sequence = s.sequence
s.log(LogInformational, "sending resume packet to gateway") s.log(LogInformational, "sending resume packet to gateway")
temp, _ := json.Marshal(p)
printJSON(temp)
err = s.wsConn.WriteJSON(p) err = s.wsConn.WriteJSON(p)
if err != nil { if err != nil {
s.log(LogWarning, "error sending gateway resume packet, %s, %s", s.gateway, err) s.log(LogWarning, "error sending gateway resume packet, %s, %s", s.gateway, err)
@ -161,16 +159,19 @@ func (s *Session) Open() (err error) {
// TODO: Add support for Voice WS/UDP connections // TODO: Add support for Voice WS/UDP connections
func (s *Session) Close() (err error) { func (s *Session) Close() (err error) {
s.log(LogInformational, "called")
s.Lock() s.Lock()
s.DataReady = false s.DataReady = false
if s.listening != nil { if s.listening != nil {
s.log(LogInformational, "closing listening channel")
close(s.listening) close(s.listening)
s.listening = nil s.listening = nil
} }
if s.wsConn != nil { if s.wsConn != nil {
s.log(LogInformational, "closing gateway websocket")
err = s.wsConn.Close() err = s.wsConn.Close()
s.wsConn = nil s.wsConn = nil
} }
@ -186,6 +187,8 @@ func (s *Session) Close() (err error) {
// listening channel is closed, or an error occurs. // listening channel is closed, or an error occurs.
func (s *Session) listen(wsConn *websocket.Conn, listening <-chan interface{}) { func (s *Session) listen(wsConn *websocket.Conn, listening <-chan interface{}) {
s.log(LogInformational, "called")
for { for {
messageType, message, err := wsConn.ReadMessage() messageType, message, err := wsConn.ReadMessage()
@ -216,8 +219,10 @@ func (s *Session) listen(wsConn *websocket.Conn, listening <-chan interface{}) {
wait := time.Duration(1) wait := time.Duration(1)
for { for {
s.log(LogInformational, "trying to reconnect to gateway")
if s.Open() == nil { if s.Open() == nil {
s.log(LogInformational, "successfully reconnected to gateway")
return return
} }
@ -255,6 +260,8 @@ type heartbeatOp struct {
// disconnect the websocket connection after a few seconds. // disconnect the websocket connection after a few seconds.
func (s *Session) heartbeat(wsConn *websocket.Conn, listening <-chan interface{}, i time.Duration) { func (s *Session) heartbeat(wsConn *websocket.Conn, listening <-chan interface{}, i time.Duration) {
s.log(LogInformational, "called")
if listening == nil || wsConn == nil { if listening == nil || wsConn == nil {
return return
} }
@ -305,6 +312,8 @@ type updateStatusOp struct {
// if otherwise, set status to active, and no game. // if otherwise, set status to active, and no game.
func (s *Session) UpdateStatus(idle int, game string) (err error) { func (s *Session) UpdateStatus(idle int, game string) (err error) {
s.log(LogInformational, "called")
s.RLock() s.RLock()
defer s.RUnlock() defer s.RUnlock()
if s.wsConn == nil { if s.wsConn == nil {
@ -370,9 +379,7 @@ func (s *Session) onEvent(messageType int, message []byte) {
return return
} }
if s.Debug { s.log(LogDebug, "Op: %d, Seq: %d, Type: %s, Data: %s\n\n", e.Operation, e.Sequence, e.Type, string(e.RawData))
s.log(LogDebug, "Op: %d, Seq: %d, Type: %s, Data: %s", e.Operation, e.Sequence, e.Type, string(e.RawData))
}
// Ping request. // Ping request.
// Must respond with a heartbeat packet within 5 seconds // Must respond with a heartbeat packet within 5 seconds
@ -385,6 +392,8 @@ func (s *Session) onEvent(messageType int, message []byte) {
s.log(LogError, "error sending heartbeat in response to Op1") s.log(LogError, "error sending heartbeat in response to Op1")
return return
} }
return
} }
// Reconnect // Reconnect
@ -405,6 +414,8 @@ func (s *Session) onEvent(messageType int, message []byte) {
s.log(LogWarning, "error sending gateway identify packet, %s, %s", s.gateway, err) s.log(LogWarning, "error sending gateway identify packet, %s, %s", s.gateway, err)
return return
} }
return
} }
// Do not try to Dispatch a non-Dispatch Message // Do not try to Dispatch a non-Dispatch Message
@ -441,7 +452,7 @@ func (s *Session) onEvent(messageType int, message []byte) {
s.handle(i) s.handle(i)
} else { } else {
s.log(LogWarning, "unknown event, %#v", e) s.log(LogWarning, "unknown event: Op: %d, Seq: %d, Type: %s, Data: %s", e.Operation, e.Sequence, e.Type, string(e.RawData))
} }
// Emit event to the OnEvent handler // Emit event to the OnEvent handler