DiscordKit: can receive and decode hello event

This commit is contained in:
2026-03-12 09:35:23 -04:00
parent d42227ba55
commit 707bbedac2
4 changed files with 66 additions and 4 deletions

View File

@@ -0,0 +1,27 @@
import Foundation
#if canImport(FoundationNetowrking)
import FoundationNetworking
#endif
struct GatewayClient {
let gatewayURL: URL
func openConnection() async throws {
let ws = URLSession.shared.webSocketTask(with: gatewayURL.appending(component: "?v=10&encoding=json"))
ws.resume()
let message = try await ws.receive()
guard case .string(let str) = message else { throw GatewayError.invalidMessage }
print(str)
let json = JSONDecoder()
let gwMessage = try json.decode(GatewayMessage.self, from: Data(str.utf8))
dump(gwMessage)
guard case .hello(let hello) = gwMessage.d else { print("whoops"); return }
dump(hello)
}
}
public enum GatewayError: Error {
case invalidMessage
case invalidOpcode
}