DiscordKit: can receive and decode hello event
This commit is contained in:
27
Sources/DiscordKit/GatewayClient.swift
Normal file
27
Sources/DiscordKit/GatewayClient.swift
Normal 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
|
||||
}
|
||||
Reference in New Issue
Block a user