From a9da8a5daf736d2bb3fa8af0265a32ff51a5ce01 Mon Sep 17 00:00:00 2001 From: Bruce Marriner Date: Sat, 30 Apr 2016 21:40:13 -0500 Subject: [PATCH] Cleanup, Logging, Finished Resumed code. --- discord.go | 8 ++++++++ events.go | 1 + structs.go | 6 ++++++ wsapi.go | 23 +++++++++++++++++------ 4 files changed, 32 insertions(+), 6 deletions(-) diff --git a/discord.go b/discord.go index 72c848f..99cc0c7 100644 --- a/discord.go +++ b/discord.go @@ -229,6 +229,7 @@ func (s *Session) initialize() { s.handlersMu.Unlock() s.AddHandler(s.onReady) + s.AddHandler(s.onResumed) s.AddHandler(s.onVoiceServerUpdate) s.AddHandler(s.onVoiceStateUpdate) 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. 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) +} diff --git a/events.go b/events.go index 23fa9be..72aabf6 100644 --- a/events.go +++ b/events.go @@ -42,6 +42,7 @@ var eventToInterface = map[string]interface{}{ "TYPING_START": TypingStart{}, "VOICE_SERVER_UPDATE": VoiceServerUpdate{}, "VOICE_STATE_UPDATE": VoiceStateUpdate{}, + "RESUMED": Resumed{}, } // Connect is an empty struct for an event. diff --git a/structs.go b/structs.go index f235043..296d28a 100644 --- a/structs.go +++ b/structs.go @@ -100,6 +100,12 @@ type rateLimitMutex struct { 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. type VoiceRegion struct { ID string `json:"id"` diff --git a/wsapi.go b/wsapi.go index 191f9f2..1b438bd 100644 --- a/wsapi.go +++ b/wsapi.go @@ -111,8 +111,6 @@ func (s *Session) Open() (err error) { p.Data.Sequence = s.sequence s.log(LogInformational, "sending resume packet to gateway") - temp, _ := json.Marshal(p) - printJSON(temp) err = s.wsConn.WriteJSON(p) if err != nil { 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 func (s *Session) Close() (err error) { + s.log(LogInformational, "called") s.Lock() s.DataReady = false if s.listening != nil { + s.log(LogInformational, "closing listening channel") close(s.listening) s.listening = nil } if s.wsConn != nil { + s.log(LogInformational, "closing gateway websocket") err = s.wsConn.Close() s.wsConn = nil } @@ -186,6 +187,8 @@ func (s *Session) Close() (err error) { // listening channel is closed, or an error occurs. func (s *Session) listen(wsConn *websocket.Conn, listening <-chan interface{}) { + s.log(LogInformational, "called") + for { messageType, message, err := wsConn.ReadMessage() @@ -216,8 +219,10 @@ func (s *Session) listen(wsConn *websocket.Conn, listening <-chan interface{}) { wait := time.Duration(1) for { + s.log(LogInformational, "trying to reconnect to gateway") if s.Open() == nil { + s.log(LogInformational, "successfully reconnected to gateway") return } @@ -255,6 +260,8 @@ type heartbeatOp struct { // disconnect the websocket connection after a few seconds. func (s *Session) heartbeat(wsConn *websocket.Conn, listening <-chan interface{}, i time.Duration) { + s.log(LogInformational, "called") + if listening == nil || wsConn == nil { return } @@ -305,6 +312,8 @@ type updateStatusOp struct { // if otherwise, set status to active, and no game. func (s *Session) UpdateStatus(idle int, game string) (err error) { + s.log(LogInformational, "called") + s.RLock() defer s.RUnlock() if s.wsConn == nil { @@ -370,9 +379,7 @@ func (s *Session) onEvent(messageType int, message []byte) { return } - if s.Debug { - s.log(LogDebug, "Op: %d, Seq: %d, Type: %s, Data: %s", e.Operation, e.Sequence, e.Type, string(e.RawData)) - } + s.log(LogDebug, "Op: %d, Seq: %d, Type: %s, Data: %s\n\n", e.Operation, e.Sequence, e.Type, string(e.RawData)) // Ping request. // 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") return } + + return } // 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) return } + + return } // Do not try to Dispatch a non-Dispatch Message @@ -441,7 +452,7 @@ func (s *Session) onEvent(messageType int, message []byte) { s.handle(i) } 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