diff options
Diffstat (limited to 'cmd/dolmetschctld')
-rw-r--r-- | cmd/dolmetschctld/web-static/socket.html | 7 | ||||
-rw-r--r-- | cmd/dolmetschctld/web_socket.go | 25 |
2 files changed, 31 insertions, 1 deletions
diff --git a/cmd/dolmetschctld/web-static/socket.html b/cmd/dolmetschctld/web-static/socket.html index 903868d..b9bebbb 100644 --- a/cmd/dolmetschctld/web-static/socket.html +++ b/cmd/dolmetschctld/web-static/socket.html @@ -49,6 +49,11 @@ ctrl.send(req); } + function set_language() { + req = { command: "language", arguments: [ $('#inputlang').val() ] }; + ctrl.send(req); + } + function init() { $('#buttonstate').attr('disabled','disabled') } @@ -60,6 +65,8 @@ <div> <button id="buttonstate" onclick="state()">get state</button> + <button id="buttonlang" onclick="set_language()">set language</button> + <input type="text" id="inputlang" length="10"> <div id="rawmsg" class="data"></div> </div> 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 } |