28 lines
801 B
Swift
28 lines
801 B
Swift
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
|
|
}
|