diff --git a/Sources/DiscordKit/GatewayClient.swift b/Sources/DiscordKit/GatewayClient.swift index ee1c5b3..4e4d89a 100644 --- a/Sources/DiscordKit/GatewayClient.swift +++ b/Sources/DiscordKit/GatewayClient.swift @@ -44,6 +44,11 @@ actor GatewayClient { while !Task.isCancelled { try await sendHeartbeat() try await Task.sleep(for: .milliseconds(helloMessage.heartbeat_interval)) + + if await !hbAck { + // haven't heard back + await ws.cancel() + } } } catch { print("Heartbeat task canceled") @@ -127,7 +132,8 @@ actor GatewayClient { private func attemptResume() async throws { guard - ws.closeCode.rawValue != 4004 && ws.closeCode.rawValue < 4010, + ws.closeCode.rawValue != 4004 && ws.closeCode.rawValue != 1000 && + ws.closeCode.rawValue != 1001 && ws.closeCode.rawValue < 4010, let resumeURL = resumeURL, let sessionID = sessionID, let sequenceNum = sequenceNum @@ -164,10 +170,6 @@ actor GatewayClient { hbAck = false let hbMessage = "{\"op\":1,\"d\":\(sequenceNum == nil ? "null" : String(sequenceNum!))}" try await ws.send(.string(hbMessage)) - try await Task.sleep(for: .seconds(5)) - if !hbAck { - ws.cancel(with: .normalClosure, reason: nil) - } } var events: AsyncStream { diff --git a/Sources/zundamon/Zundamon.swift b/Sources/zundamon/Zundamon.swift index 939dc72..4dd3861 100644 --- a/Sources/zundamon/Zundamon.swift +++ b/Sources/zundamon/Zundamon.swift @@ -23,13 +23,21 @@ struct Zundamon { try await bot.connect() - for await event in await bot.events { - switch event { - case .messageCreate(let event): - try await MessageHandler(ctx: event, client: bot.client).handle() - default: - continue + await withThrowingTaskGroup(of: Void.self) { group in + for i in 0...5 { + group.addTask { + for await event in await bot.events { + print(i) + switch event { + case .messageCreate(let event): + try await MessageHandler(ctx: event, client: bot.client).handle() + default: + continue + } + } + } } + } print("bottom of main") }