From e6c7abc969b3f59d24f4da8796da9829b332cc71 Mon Sep 17 00:00:00 2001 From: Stephen McQuay Date: Mon, 10 Mar 2014 21:58:36 -0700 Subject: [PATCH] Implemented Fraser's suggestion Typically client offers supported encodings, then the server chooses the optimal one. --- protocol.go | 40 +++++++++++++++++++++++++++------------- 1 file changed, 27 insertions(+), 13 deletions(-) diff --git a/protocol.go b/protocol.go index 959b4df..0cb79d5 100644 --- a/protocol.go +++ b/protocol.go @@ -200,23 +200,37 @@ func addPlayer(ws *websocket.Conn) { return } - err = websocket.JSON.Send(ws, - []string{"json", "jsonz", "gob"}, - ) + reqEncs := []string{} + err = websocket.JSON.Receive(ws, &reqEncs) if err != nil { - 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) + log.Printf("%s %s unable to parse requested encodings", gid.Id, player_id) websocket.JSON.Send(ws, NewFailure("encoding recieve error")) return } - log.Printf("negociated encoding: %s", encoding) - // TODO: use this negociated encoding + prefEncs := []string{ + "gob", + "json", + } + + var encoding string +encodingLoops: + for _, prefEnc := range prefEncs { + for _, reqEnc := range reqEncs { + if reqEnc == prefEnc { + encoding = prefEnc + log.Println("selected following encoding:", encoding) + break encodingLoops + } + } + } + if encoding == "" { + log.Printf("%s %s unable to negociate encoding", gid.Id, player_id) + websocket.JSON.Send( + ws, + NewFailure("no overlap on supported encodings; I suggest using json"), + ) + return + } gameParam := game.gameParam() gp := struct {