From f645c42eb3629a402fcd878899f622011d8a9b24 Mon Sep 17 00:00:00 2001 From: Stephen McQuay Date: Sun, 9 Mar 2014 23:58:56 -0700 Subject: [PATCH] Added encoding negociation step into protocol. This is the first step involved in letting go clients talk gob to the game server. Still missing: actually use the client's prefered encoding --- protocol.go | 33 +++++++++++++++++++++++++++++---- 1 file changed, 29 insertions(+), 4 deletions(-) diff --git a/protocol.go b/protocol.go index 4e76d92..959b4df 100644 --- a/protocol.go +++ b/protocol.go @@ -200,11 +200,36 @@ func addPlayer(ws *websocket.Conn) { return } - gameParam := game.gameParam() - err = websocket.JSON.Send(ws, gameParam) + err = websocket.JSON.Send(ws, + []string{"json", "jsonz", "gob"}, + ) if err != nil { - log.Printf("%s %s game param parse error", gid.Id, player_id) - websocket.JSON.Send(ws, NewFailure("game param parse error")) + log.Printf("%s %s unable to send supported encodings msg", gid.Id, player_id) + websocket.JSON.Send(ws, NewFailure("encoding send error")) + return + } + var encoding string + err = websocket.JSON.Receive(ws, &encoding) + if err != nil { + log.Printf("%s %s unable to parse encoding", gid.Id, player_id) + websocket.JSON.Send(ws, NewFailure("encoding recieve error")) + return + } + log.Printf("negociated encoding: %s", encoding) + // TODO: use this negociated encoding + + gameParam := game.gameParam() + gp := struct { + GameParam + Encoding string `json:"encoding"` + }{ + GameParam: *gameParam, + Encoding: encoding, + } + err = websocket.JSON.Send(ws, gp) + if err != nil { + log.Printf("%s %s game param send error", gid.Id, player_id) + websocket.JSON.Send(ws, NewFailure("game param send error")) return } else { log.Printf("%s -> %s: sent %+v", gid.Id, player_id, gameParam)