diff options
Diffstat (limited to 'cmd/dolmetschctld/web_socket.go')
-rw-r--r-- | cmd/dolmetschctld/web_socket.go | 25 |
1 files changed, 24 insertions, 1 deletions
diff --git a/cmd/dolmetschctld/web_socket.go b/cmd/dolmetschctld/web_socket.go index 2e90b70..9b5d74e 100644 --- a/cmd/dolmetschctld/web_socket.go +++ b/cmd/dolmetschctld/web_socket.go @@ -69,6 +69,27 @@ func sendWebSocketErrorResponse(ws *websocket.Conn, code int, errStr string) { func webSocketHandleCommand(ws *websocket.Conn, sm *StateMachine, req webSocketRequest) { switch req.Command { case "state": + if len(req.Args) != 0 { + sendWebSocketErrorResponse(ws, http.StatusBadRequest, "command 'state' expects no arguments") + return + } + + var result webSocketResponseState + result.ResponseCode = http.StatusOK + result.Type = "state" + result.State, result.Ratio, result.Lang = sm.GetState() + sendWebSocketResponse(ws, result) + case "language": + if len(req.Args) != 1 { + sendWebSocketErrorResponse(ws, http.StatusBadRequest, "command 'language' expects exatly one argument") + return + } + + if err := sm.SetLanguage(Language(req.Args[0])); err != nil { + sendWebSocketErrorResponse(ws, http.StatusBadRequest, err.Error()) + return + } + var result webSocketResponseState result.ResponseCode = http.StatusOK result.Type = "state" @@ -100,7 +121,9 @@ func webSocketHandler(sm *StateMachine, w http.ResponseWriter, r *http.Request) switch t { case websocket.TextMessage: var req webSocketRequest - if err := json.NewDecoder(r).Decode(&req); err != nil { + dec := json.NewDecoder(r) + dec.DisallowUnknownFields() + if err := dec.Decode(&req); err != nil { if err == io.EOF { err = io.ErrUnexpectedEOF } |