partial support for gateway resume

This commit is contained in:
Bruce Marriner 2016-04-30 17:52:09 -05:00
parent b09ed37294
commit 9bc6057ca7

View file

@ -48,6 +48,15 @@ type handshakeOp struct {
Data handshakeData `json:"d"` Data handshakeData `json:"d"`
} }
type ResumePacket struct {
Op int `json:"ip"`
Data struct {
Token string `json:"token"`
SessionID string `json:"session_id"`
Sequence int `json:"seq"`
} `json:"d"`
}
// Open opens a websocket connection to Discord. // Open opens a websocket connection to Discord.
func (s *Session) Open() (err error) { func (s *Session) Open() (err error) {
@ -95,10 +104,22 @@ func (s *Session) Open() (err error) {
if s.sessionID != "" && s.sequence > 0 { if s.sessionID != "" && s.sequence > 0 {
p := ResumePacket{}
p.Op = 6
p.Data.Token = s.Token
p.Data.SessionID = s.sessionID
p.Data.Sequence = s.sequence
s.log(LogInformational, "sending resume packet to gateway") s.log(LogInformational, "sending resume packet to gateway")
// TODO: RESUME 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)
return
} }
//else {
} else {
s.log(LogInformational, "sending identify packet to gateway") s.log(LogInformational, "sending identify packet to gateway")
err = s.wsConn.WriteJSON(handshakeOp{2, handshakeData{s.Token, handshakeProperties{runtime.GOOS, "Discordgo v" + VERSION, "", "", ""}, 250, s.Compress}}) err = s.wsConn.WriteJSON(handshakeOp{2, handshakeData{s.Token, handshakeProperties{runtime.GOOS, "Discordgo v" + VERSION, "", "", ""}, 250, s.Compress}})
@ -106,7 +127,7 @@ func (s *Session) Open() (err error) {
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
} }
//} }
// Create listening outside of listen, as it needs to happen inside the mutex // Create listening outside of listen, as it needs to happen inside the mutex
// lock. // lock.
@ -326,6 +347,24 @@ func (s *Session) onEvent(messageType int, message []byte) {
} }
} }
// Reconnect
// Must immediately disconnect from gateway and reconnect to new gateway.
if e.Operation == 7 {
// TODO
}
// Invalid Session
// Must respond with a Identify packet.
if e.Operation == 9 {
s.log(LogInformational, "sending identify packet to gateway in response to Op9")
err = s.wsConn.WriteJSON(handshakeOp{2, handshakeData{s.Token, handshakeProperties{runtime.GOOS, "Discordgo v" + VERSION, "", "", ""}, 250, s.Compress}})
if err != nil {
s.log(LogWarning, "error sending gateway identify packet, %s, %s", s.gateway, err)
return
}
}
// Do not try to Dispatch a non-Dispatch Message // Do not try to Dispatch a non-Dispatch Message
if e.Operation != 0 { if e.Operation != 0 {
// But we probably should be doing something with them. // But we probably should be doing something with them.