@@ -101,6 +101,7 @@ public class RobustWebSocket: NSObject {
101101 // MARK: - Configuration
102102 internal let token : String
103103 private let reconnectInterval : ReconnectDelayClosure
104+ private var reconnectURL : URL ?
104105
105106 /// The gateway close codes that signal a fatal error, and reconnection shouldn't be attempted
106107 private static let fatalCloseCodes = [ 4004 ] + Array( 4010 ... 4014 )
@@ -259,21 +260,23 @@ public class RobustWebSocket: NSObject {
259260 }
260261 #endif
261262
263+ let connectionURL = reconnectURL ?? URL ( string: DiscordKitConfig . default. gateway) !
262264 Self . log. info ( " [CONNECT] " , metadata: [
263265 " ws " : " \( DiscordKitConfig . default. gateway) " ,
264- " version " : " \( DiscordKitConfig . default. version) "
266+ " version " : " \( DiscordKitConfig . default. version) " ,
267+ " url " : " \( connectionURL) "
265268 ] )
266269 pendingReconnect = nil
267270
268271 #if canImport(WebSocket)
269272 socket = WebSocket ( )
270273 do {
271- try socket. connect ( to : DiscordKitConfig . default . gateway , headers: HTTPHeaders ( dictionaryLiteral: ( " User-Agent " , DiscordKitConfig . default. userAgent) ) )
274+ try socket. connect ( url : connectionURL , headers: HTTPHeaders ( dictionaryLiteral: ( " User-Agent " , DiscordKitConfig . default. userAgent) ) )
272275 } catch {
273276 Self . log. critical ( " Failed to connect to Gateway " , metadata: [ " Reason " : " \( error. localizedDescription) " ] )
274277 }
275278 #else
276- var gatewayReq = URLRequest ( url: URL ( string : DiscordKitConfig . default . gateway ) ! )
279+ var gatewayReq = URLRequest ( url: connectionURL )
277280 // The difference in capitalisation is intentional
278281 gatewayReq. setValue ( DiscordKitConfig . default. userAgent, forHTTPHeaderField: " User-Agent " )
279282 socket = session. webSocketTask ( with: gatewayReq)
@@ -355,6 +358,7 @@ public class RobustWebSocket: NSObject {
355358 Self . log. warning ( " [RECONNECT] Session is invalid, reconnecting without resuming " )
356359 onSessionInvalid. notify ( )
357360 canResume = false
361+ reconnectURL = nil
358362 }
359363 // Close the connection immediately and reconnect after 1-5s, as per Discord docs
360364 // Unfortunately Discord seems to reject the new identify no matter how long I
@@ -368,15 +372,25 @@ public class RobustWebSocket: NSObject {
368372 // attemptReconnect(resume: shouldResume)
369373 case . userReady( let ready) :
370374 sessionID = ready. session_id
375+ reconnectURL = ready. resume_gateway_url
371376 canResume = true
372377 sessionOpen = true
378+ Self . log. info ( " [READY] " , metadata: [
379+ " session " : " \( ready. session_id) " ,
380+ " reconnectURL " : " \( ready. resume_gateway_url) "
381+ ] )
373382 case . botReady( let ready) :
374383 sessionID = ready. session_id
375384 canResume = true
376- Self . log. info ( " [READY] " , metadata: [ " session " : " \( ready. session_id) " ] )
377- fallthrough
385+ sessionOpen = true
386+ reconnectURL = ready. resume_gateway_url
387+ Self . log. info ( " [READY] " , metadata: [
388+ " session " : " \( ready. session_id) " ,
389+ " reconnectURL " : " \( ready. resume_gateway_url) "
390+ ] )
378391 case . resumed:
379392 sessionOpen = true
393+ Self . log. info ( " [RESUMED] " )
380394 // onEvent.notify(event: (type, decoded.data))
381395 case . reconnect:
382396 Self . log. warning ( " Gateway-requested reconnect: disconnecting and reconnecting immediately " )
0 commit comments