Force Voice reconnect on gateway reconnects

Also did a bit of clean up.
This commit is contained in:
Bruce Marriner 2016-06-14 10:55:20 -05:00
parent d59000f544
commit c32de41481

View file

@ -26,8 +26,6 @@ import (
"github.com/gorilla/websocket" "github.com/gorilla/websocket"
) )
var GATEWAY_VERSION int = 4
type handshakeProperties struct { type handshakeProperties struct {
OS string `json:"$os"` OS string `json:"$os"`
Browser string `json:"$browser"` Browser string `json:"$browser"`
@ -48,7 +46,7 @@ type handshakeOp struct {
Data handshakeData `json:"d"` Data handshakeData `json:"d"`
} }
type ResumePacket struct { type resumePacket struct {
Op int `json:"op"` Op int `json:"op"`
Data struct { Data struct {
Token string `json:"token"` Token string `json:"token"`
@ -87,7 +85,7 @@ func (s *Session) Open() (err error) {
} }
// Add the version and encoding to the URL // Add the version and encoding to the URL
s.gateway = fmt.Sprintf("%s?v=%v&encoding=json", s.gateway, GATEWAY_VERSION) s.gateway = fmt.Sprintf("%s?v=4&encoding=json", s.gateway)
} }
header := http.Header{} header := http.Header{}
@ -104,7 +102,7 @@ func (s *Session) Open() (err error) {
if s.sessionID != "" && s.sequence > 0 { if s.sessionID != "" && s.sequence > 0 {
p := ResumePacket{} p := resumePacket{}
p.Op = 6 p.Op = 6
p.Data.Token = s.Token p.Data.Token = s.Token
p.Data.SessionID = s.sessionID p.Data.SessionID = s.sessionID
@ -320,7 +318,7 @@ func (s *Session) onEvent(messageType int, message []byte) {
reader = bytes.NewBuffer(message) reader = bytes.NewBuffer(message)
// If this is a compressed message, uncompress it. // If this is a compressed message, uncompress it.
if messageType == 2 { if messageType == websocket.BinaryMessage {
z, err := zlib.NewReader(reader) z, err := zlib.NewReader(reader)
if err != nil { if err != nil {
@ -591,14 +589,21 @@ func (s *Session) reconnect() {
if err == nil { if err == nil {
s.log(LogInformational, "successfully reconnected to gateway") s.log(LogInformational, "successfully reconnected to gateway")
/* // I'm not sure if this is actually needed.
// I'm not sure if this is actually needed. // if the gw reconnect works properly, voice should stay alive
// if the gw reconnect works properly, voice should stay alive // However, there seems to be cases where something "weird"
for _, v := range s.VoiceConnections { // happens. So we're doing this for now just to improve
s.log(LogInformational, "reconnecting voice connection to guild %s", v.GuildID) // stability in those edge cases.
go v.reconnect() for _, v := range s.VoiceConnections {
}
*/ s.log(LogInformational, "reconnecting voice connection to guild %s", v.GuildID)
go v.reconnect()
// This is here just to prevent violently spamming the
// voice reconnects
time.Sleep(1 * time.Second)
}
return return
} }