Skip to content

Commit 339ced9

Browse files
feat(RobustWebSocket): reconnect to resume URL if possible
Apparently this is required to improve the chances of a successful resume patch(RobustWebSocket): more consistent logging
1 parent b93364c commit 339ced9

File tree

2 files changed

+24
-6
lines changed

2 files changed

+24
-6
lines changed

Sources/DiscordKitCore/Gateway/RobustWebSocket.swift

Lines changed: 19 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -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")

Sources/DiscordKitCore/Objects/Gateway/Event/ReadyEvt.swift

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,10 @@ public struct ReadyEvt: Decodable, GatewayData {
2727
///
2828
/// > An implementation for unreads is still WIP in Swiftcord
2929
public let read_state: ReadState
30+
31+
public let auth_token: String?
32+
33+
public let resume_gateway_url: URL
3034
}
3135

3236
/// The ready event payload for bot accounts
@@ -38,5 +42,5 @@ public struct BotReadyEvt: Decodable, GatewayData {
3842
public let session_id: String
3943
public let shard: [Int]? // Included for inclusivity, will not be used
4044
public let application: PartialApplication
41-
public let resume_gateway_url: String
45+
public let resume_gateway_url: URL
4246
}

0 commit comments

Comments
 (0)