Implemented Fraser's suggestion

Typically client offers supported encodings, then the server chooses the
optimal one.
This commit is contained in:
Stephen McQuay 2014-03-10 21:58:36 -07:00
parent f645c42eb3
commit e6c7abc969

View File

@ -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 {