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 {