@@ -62,22 +62,32 @@ func (s *Session) Open() error {
62
62
return ErrWSAlreadyOpen
63
63
}
64
64
65
+ sequence := atomic .LoadInt64 (s .sequence )
66
+
67
+ var gateway string
65
68
// Get the gateway to use for the Websocket connection
66
- if s .gateway == "" {
67
- s .gateway , err = s .Gateway ()
68
- if err != nil {
69
- return err
69
+ if sequence != 0 && s .sessionID != "" && s .resumeGatewayURL != "" {
70
+ s .log (LogDebug , "using resume gateway %s" , s .resumeGatewayURL )
71
+ gateway = s .resumeGatewayURL
72
+ } else {
73
+ if s .gateway == "" {
74
+ s .gateway , err = s .Gateway ()
75
+ if err != nil {
76
+ return err
77
+ }
70
78
}
71
79
72
- // Add the version and encoding to the URL
73
- s .gateway = s .gateway + "?v=" + APIVersion + "&encoding=json"
80
+ gateway = s .gateway
74
81
}
75
82
83
+ // Add the version and encoding to the URL
84
+ gateway += "?v=" + APIVersion + "&encoding=json"
85
+
76
86
// Connect to the Gateway
77
- s .log (LogInformational , "connecting to gateway %s" , s . gateway )
87
+ s .log (LogInformational , "connecting to gateway %s" , gateway )
78
88
header := http.Header {}
79
89
header .Add ("accept-encoding" , "zlib" )
80
- s .wsConn , _ , err = s .Dialer .Dial (s . gateway , header )
90
+ s .wsConn , _ , err = s .Dialer .Dial (gateway , header )
81
91
if err != nil {
82
92
s .log (LogError , "error connecting to gateway %s, %s" , s .gateway , err )
83
93
s .gateway = "" // clear cached gateway
@@ -123,7 +133,6 @@ func (s *Session) Open() error {
123
133
124
134
// Now we send either an Op 2 Identity if this is a brand new
125
135
// connection or Op 6 Resume if we are resuming an existing connection.
126
- sequence := atomic .LoadInt64 (s .sequence )
127
136
if s .sessionID == "" && sequence == 0 {
128
137
129
138
// Send Op 2 Identity Packet
@@ -616,15 +625,23 @@ func (s *Session) onEvent(messageType int, message []byte) (*Event, error) {
616
625
// Invalid Session
617
626
// Must respond with a Identify packet.
618
627
if e .Operation == 9 {
628
+ s .log (LogInformational , "Closing and reconnecting in response to Op9" )
629
+ s .CloseWithCode (websocket .CloseServiceRestart )
619
630
620
- s .log (LogInformational , "sending identify packet to gateway in response to Op9" )
621
-
622
- err = s .identify ()
623
- if err != nil {
624
- s .log (LogWarning , "error sending gateway identify packet, %s, %s" , s .gateway , err )
631
+ var resumable bool
632
+ if err := json .Unmarshal (e .RawData , & resumable ); err != nil {
633
+ s .log (LogError , "error unmarshalling invalid session event, %s" , err )
625
634
return e , err
626
635
}
627
636
637
+ if ! resumable {
638
+ s .log (LogInformational , "Gateway session is not resumable, discarding its information" )
639
+ s .resumeGatewayURL = ""
640
+ s .sessionID = ""
641
+ atomic .StoreInt64 (s .sequence , 0 )
642
+ }
643
+
644
+ s .reconnect ()
628
645
return e , nil
629
646
}
630
647
0 commit comments