Merge pull request #761 from bwmarrin/reconnect-fix

Allow resume on reconnect
This commit is contained in:
Carson Hoffman 2020-04-11 23:05:08 -04:00 committed by GitHub
commit d8b49998f8
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23

View file

@ -516,7 +516,7 @@ func (s *Session) onEvent(messageType int, message []byte) (*Event, error) {
// Must immediately disconnect from gateway and reconnect to new gateway. // Must immediately disconnect from gateway and reconnect to new gateway.
if e.Operation == 7 { if e.Operation == 7 {
s.log(LogInformational, "Closing and reconnecting in response to Op7") s.log(LogInformational, "Closing and reconnecting in response to Op7")
s.Close() s.CloseWithCode(websocket.CloseServiceRestart)
s.reconnect() s.reconnect()
return e, nil return e, nil
} }
@ -838,9 +838,13 @@ func (s *Session) reconnect() {
} }
} }
func (s *Session) Close() error {
return s.CloseWithCode(websocket.CloseNormalClosure)
}
// Close closes a websocket and stops all listening/heartbeat goroutines. // Close closes a websocket and stops all listening/heartbeat goroutines.
// TODO: Add support for Voice WS/UDP connections // TODO: Add support for Voice WS/UDP connections
func (s *Session) Close() (err error) { func (s *Session) CloseWithCode(closeCode int) (err error) {
s.log(LogInformational, "called") s.log(LogInformational, "called")
s.Lock() s.Lock()
@ -862,7 +866,7 @@ func (s *Session) Close() (err error) {
// To cleanly close a connection, a client should send a close // To cleanly close a connection, a client should send a close
// frame and wait for the server to close the connection. // frame and wait for the server to close the connection.
s.wsMutex.Lock() s.wsMutex.Lock()
err := s.wsConn.WriteMessage(websocket.CloseMessage, websocket.FormatCloseMessage(websocket.CloseNormalClosure, "")) err := s.wsConn.WriteMessage(websocket.CloseMessage, websocket.FormatCloseMessage(closeCode, ""))
s.wsMutex.Unlock() s.wsMutex.Unlock()
if err != nil { if err != nil {
s.log(LogInformational, "error closing websocket, %s", err) s.log(LogInformational, "error closing websocket, %s", err)