Switch to V5 gateway (#300)

This switch the gateway to V5, and change the heartbeat logic to
get the heartbeat_interval from the new Hello opcode instant of
READY/RESUME event.
See: https://github.com/hammerandchisel/discord-api-docs/issues/18

Fix: #220
This commit is contained in:
Kristian Klausen 2016-12-26 01:22:14 +01:00 committed by Chris Rhodes
parent 3fdc5f373d
commit b377944b97
4 changed files with 26 additions and 27 deletions

View file

@ -211,8 +211,6 @@ func (s *Session) onInterface(i interface{}) {
setGuildIds(t.Guild)
case *GuildUpdate:
setGuildIds(t.Guild)
case *Resumed:
s.onResumed(t)
case *VoiceServerUpdate:
go s.onVoiceServerUpdate(t)
case *VoiceStateUpdate:
@ -229,14 +227,4 @@ func (s *Session) onReady(r *Ready) {
// Store the SessionID within the Session struct.
s.sessionID = r.SessionID
// 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(r *Resumed) {
// Start the heartbeat to keep the connection alive.
go s.heartbeat(s.wsConn, s.listening, r.HeartbeatInterval)
}

View file

@ -2,7 +2,6 @@ package discordgo
import (
"encoding/json"
"time"
)
// This file contains all the possible structs that can be
@ -39,7 +38,6 @@ type Event struct {
type Ready struct {
Version int `json:"v"`
SessionID string `json:"session_id"`
HeartbeatInterval time.Duration `json:"heartbeat_interval"`
User *User `json:"user"`
ReadState []*ReadState `json:"read_state"`
PrivateChannels []*Channel `json:"private_channels"`
@ -191,7 +189,6 @@ type PresenceUpdate struct {
// Resumed is the data for a Resumed event.
type Resumed struct {
HeartbeatInterval time.Duration `json:"heartbeat_interval"`
Trace []string `json:"_trace"`
}

View file

@ -611,7 +611,6 @@ func (s *State) onReady(se *Session, r *Ready) (err error) {
ready := Ready{
Version: r.Version,
SessionID: r.SessionID,
HeartbeatInterval: r.HeartbeatInterval,
User: r.User,
}

View file

@ -74,7 +74,7 @@ func (s *Session) Open() (err error) {
}
// Add the version and encoding to the URL
s.gateway = fmt.Sprintf("%s?v=4&encoding=json", s.gateway)
s.gateway = fmt.Sprintf("%s?v=5&encoding=json", s.gateway)
}
header := http.Header{}
@ -180,6 +180,11 @@ type heartbeatOp struct {
Data int `json:"d"`
}
type helloOp struct {
HeartbeatInterval time.Duration `json:"heartbeat_interval"`
Trace []string `json:"_trace"`
}
// heartbeat sends regular heartbeats to Discord so it knows the client
// is still connected. If you do not send these heartbeats Discord will
// disconnect the websocket connection after a few seconds.
@ -396,6 +401,16 @@ func (s *Session) onEvent(messageType int, message []byte) {
return
}
if e.Operation == 10 {
var h helloOp
if err = json.Unmarshal(e.RawData, &h); err != nil {
s.log(LogError, "error unmarshalling helloOp, %s", err)
} else {
go s.heartbeat(s.wsConn, s.listening, h.HeartbeatInterval)
}
return
}
// Do not try to Dispatch a non-Dispatch Message
if e.Operation != 0 {
// But we probably should be doing something with them.