mirror of
https://github.com/bwmarrin/discordgo.git
synced 2025-12-23 23:17:45 -05:00
feat(Session): resume gateway url (#1307)
This commit is contained in:
3
event.go
3
event.go
@@ -244,4 +244,7 @@ func (s *Session) onReady(r *Ready) {
|
|||||||
|
|
||||||
// Store the SessionID within the Session struct.
|
// Store the SessionID within the Session struct.
|
||||||
s.sessionID = r.SessionID
|
s.sessionID = r.SessionID
|
||||||
|
|
||||||
|
// Store the ResumeGatewayURL within the Session struct.
|
||||||
|
s.resumeGatewayURL = r.ResumeGatewayURL
|
||||||
}
|
}
|
||||||
|
|||||||
15
events.go
15
events.go
@@ -36,13 +36,14 @@ type Event struct {
|
|||||||
|
|
||||||
// A Ready stores all data for the websocket READY event.
|
// A Ready stores all data for the websocket READY event.
|
||||||
type Ready struct {
|
type Ready struct {
|
||||||
Version int `json:"v"`
|
Version int `json:"v"`
|
||||||
SessionID string `json:"session_id"`
|
SessionID string `json:"session_id"`
|
||||||
User *User `json:"user"`
|
User *User `json:"user"`
|
||||||
Shard *[2]int `json:"shard"`
|
Shard *[2]int `json:"shard"`
|
||||||
Application *Application `json:"application"`
|
ResumeGatewayURL string `json:"resume_gateway_url"`
|
||||||
Guilds []*Guild `json:"guilds"`
|
Application *Application `json:"application"`
|
||||||
PrivateChannels []*Channel `json:"private_channels"`
|
Guilds []*Guild `json:"guilds"`
|
||||||
|
PrivateChannels []*Channel `json:"private_channels"`
|
||||||
}
|
}
|
||||||
|
|
||||||
// ChannelCreate is the data for a ChannelCreate event.
|
// ChannelCreate is the data for a ChannelCreate event.
|
||||||
|
|||||||
@@ -126,6 +126,9 @@ type Session struct {
|
|||||||
// sequence tracks the current gateway api websocket sequence number
|
// sequence tracks the current gateway api websocket sequence number
|
||||||
sequence *int64
|
sequence *int64
|
||||||
|
|
||||||
|
// stores sessions current Discord Resume Gateway
|
||||||
|
resumeGatewayURL string
|
||||||
|
|
||||||
// stores sessions current Discord Gateway
|
// stores sessions current Discord Gateway
|
||||||
gateway string
|
gateway string
|
||||||
|
|
||||||
|
|||||||
45
wsapi.go
45
wsapi.go
@@ -62,22 +62,32 @@ func (s *Session) Open() error {
|
|||||||
return ErrWSAlreadyOpen
|
return ErrWSAlreadyOpen
|
||||||
}
|
}
|
||||||
|
|
||||||
|
sequence := atomic.LoadInt64(s.sequence)
|
||||||
|
|
||||||
|
var gateway string
|
||||||
// Get the gateway to use for the Websocket connection
|
// Get the gateway to use for the Websocket connection
|
||||||
if s.gateway == "" {
|
if sequence != 0 && s.sessionID != "" && s.resumeGatewayURL != "" {
|
||||||
s.gateway, err = s.Gateway()
|
s.log(LogDebug, "using resume gateway %s", s.resumeGatewayURL)
|
||||||
if err != nil {
|
gateway = s.resumeGatewayURL
|
||||||
return err
|
} else {
|
||||||
|
if s.gateway == "" {
|
||||||
|
s.gateway, err = s.Gateway()
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// Add the version and encoding to the URL
|
gateway = s.gateway
|
||||||
s.gateway = s.gateway + "?v=" + APIVersion + "&encoding=json"
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Add the version and encoding to the URL
|
||||||
|
gateway += "?v=" + APIVersion + "&encoding=json"
|
||||||
|
|
||||||
// Connect to the Gateway
|
// Connect to the Gateway
|
||||||
s.log(LogInformational, "connecting to gateway %s", s.gateway)
|
s.log(LogInformational, "connecting to gateway %s", gateway)
|
||||||
header := http.Header{}
|
header := http.Header{}
|
||||||
header.Add("accept-encoding", "zlib")
|
header.Add("accept-encoding", "zlib")
|
||||||
s.wsConn, _, err = s.Dialer.Dial(s.gateway, header)
|
s.wsConn, _, err = s.Dialer.Dial(gateway, header)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
s.log(LogError, "error connecting to gateway %s, %s", s.gateway, err)
|
s.log(LogError, "error connecting to gateway %s, %s", s.gateway, err)
|
||||||
s.gateway = "" // clear cached gateway
|
s.gateway = "" // clear cached gateway
|
||||||
@@ -123,7 +133,6 @@ func (s *Session) Open() error {
|
|||||||
|
|
||||||
// Now we send either an Op 2 Identity if this is a brand new
|
// Now we send either an Op 2 Identity if this is a brand new
|
||||||
// connection or Op 6 Resume if we are resuming an existing connection.
|
// connection or Op 6 Resume if we are resuming an existing connection.
|
||||||
sequence := atomic.LoadInt64(s.sequence)
|
|
||||||
if s.sessionID == "" && sequence == 0 {
|
if s.sessionID == "" && sequence == 0 {
|
||||||
|
|
||||||
// Send Op 2 Identity Packet
|
// Send Op 2 Identity Packet
|
||||||
@@ -616,15 +625,23 @@ func (s *Session) onEvent(messageType int, message []byte) (*Event, error) {
|
|||||||
// Invalid Session
|
// Invalid Session
|
||||||
// Must respond with a Identify packet.
|
// Must respond with a Identify packet.
|
||||||
if e.Operation == 9 {
|
if e.Operation == 9 {
|
||||||
|
s.log(LogInformational, "Closing and reconnecting in response to Op9")
|
||||||
|
s.CloseWithCode(websocket.CloseServiceRestart)
|
||||||
|
|
||||||
s.log(LogInformational, "sending identify packet to gateway in response to Op9")
|
var resumable bool
|
||||||
|
if err := json.Unmarshal(e.RawData, &resumable); err != nil {
|
||||||
err = s.identify()
|
s.log(LogError, "error unmarshalling invalid session event, %s", err)
|
||||||
if err != nil {
|
|
||||||
s.log(LogWarning, "error sending gateway identify packet, %s, %s", s.gateway, err)
|
|
||||||
return e, err
|
return e, err
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if !resumable {
|
||||||
|
s.log(LogInformational, "Gateway session is not resumable, discarding its information")
|
||||||
|
s.resumeGatewayURL = ""
|
||||||
|
s.sessionID = ""
|
||||||
|
atomic.StoreInt64(s.sequence, 0)
|
||||||
|
}
|
||||||
|
|
||||||
|
s.reconnect()
|
||||||
return e, nil
|
return e, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user