33 lines
717 B
Swift
33 lines
717 B
Swift
|
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)
|
||
|
}
|
||
|
}
|