Schafkopf-Server/Sources/App/Management/ClientConnection.swift

33 lines
717 B
Swift
Raw Normal View History

2021-12-01 22:47:19 +01:00
import Foundation
import WebSocketKit
private let encoder = JSONEncoder()
enum ClientMessageType: String {
/// The names and connection states of th player, plus table name and id
case tableInfo = "t"
/// The hand cards of the player and the cards on the table
case cardInfo = "c"
/// The game is in the bidding phase
case biddingInfo = "b"
///
}
protocol ClientMessage: Encodable {
static var type: ClientMessageType { get }
}
extension WebSocket {
func send<T>(_ data: T) where T: ClientMessage {
let json = try! encoder.encode(data)
let string = String(data: json, encoding: .utf8)!
self.send(T.type.rawValue + string)
}
}