Browse Source

Cleanup, Logging, Finished Resumed code.

Bruce Marriner 8 years ago
parent
commit
a9da8a5daf
4 changed files with 32 additions and 6 deletions
  1. 8 0
      discord.go
  2. 1 0
      events.go
  3. 6 0
      structs.go
  4. 17 6
      wsapi.go

+ 8 - 0
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)
+}

+ 1 - 0
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.

+ 6 - 0
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"`

+ 17 - 6
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