diff options
Diffstat (limited to 'cmd/dolmetschctld/web_socket.go')
-rw-r--r-- | cmd/dolmetschctld/web_socket.go | 58 |
1 files changed, 17 insertions, 41 deletions
diff --git a/cmd/dolmetschctld/web_socket.go b/cmd/dolmetschctld/web_socket.go index f904a1c..09f2efc 100644 --- a/cmd/dolmetschctld/web_socket.go +++ b/cmd/dolmetschctld/web_socket.go @@ -30,33 +30,9 @@ import ( "net/http" "github.com/gorilla/websocket" + "spreadspace.org/dolmetschctl/pkg/types" ) -type webSocketRequest struct { - Command string `json:"command"` - Args []string `json:"arguments"` -} - -type webSocketResponseBase struct { - ResponseCode int `json:"response-code"` - Type string `json:"type"` -} - -type webSocketResponseError struct { - webSocketResponseBase - ErrorString string `json:"error"` -} - -type webSocketResponseState struct { - webSocketResponseBase - webState -} - -type webSocketResponseLanguages struct { - webSocketResponseBase - Languages []Language `json:"languages"` -} - func sendWebSocketResponse(ws *websocket.Conn, rd interface{}) { if err := ws.WriteJSON(rd); err != nil { log.Println("Web(socket) client", ws.RemoteAddr(), "write error:", err) @@ -64,14 +40,14 @@ func sendWebSocketResponse(ws *websocket.Conn, rd interface{}) { } func sendWebSocketErrorResponse(ws *websocket.Conn, code int, errStr string) { - rd := &webSocketResponseError{} + rd := &types.WebSocketResponseError{} rd.ResponseCode = code rd.Type = "error" rd.ErrorString = errStr sendWebSocketResponse(ws, rd) } -func webSocketHandleRequest(ws *websocket.Conn, sm *StateMachine, req webSocketRequest, subCh chan<- FullState) { +func webSocketHandleRequest(ws *websocket.Conn, sm *StateMachine, req types.WebSocketRequest, subCh chan<- types.FullState) { switch req.Command { case "state": if len(req.Args) != 0 { @@ -79,7 +55,7 @@ func webSocketHandleRequest(ws *websocket.Conn, sm *StateMachine, req webSocketR return } - var result webSocketResponseState + var result types.WebSocketResponseState result.ResponseCode = http.StatusOK result.Type = "state" result.State, result.Ratio, result.Lang = sm.GetState() @@ -95,7 +71,7 @@ func webSocketHandleRequest(ws *websocket.Conn, sm *StateMachine, req webSocketR return } - var result webSocketResponseState + var result types.WebSocketResponseState result.ResponseCode = http.StatusOK result.Type = "state" result.State, result.Ratio, result.Lang = sm.GetState() @@ -106,7 +82,7 @@ func webSocketHandleRequest(ws *websocket.Conn, sm *StateMachine, req webSocketR return } - var result webSocketResponseLanguages + var result types.WebSocketResponseLanguages result.ResponseCode = http.StatusOK result.Type = "languages" result.Languages = sm.GetLanguages() @@ -117,12 +93,12 @@ func webSocketHandleRequest(ws *websocket.Conn, sm *StateMachine, req webSocketR return } - if err := sm.SetLanguage(Language(req.Args[0])); err != nil { + if err := sm.SetLanguage(types.Language(req.Args[0])); err != nil { sendWebSocketErrorResponse(ws, http.StatusBadRequest, err.Error()) return } - var result webSocketResponseState + var result types.WebSocketResponseState result.ResponseCode = http.StatusOK result.Type = "state" result.State, result.Ratio, result.Lang = sm.GetState() @@ -132,22 +108,22 @@ func webSocketHandleRequest(ws *websocket.Conn, sm *StateMachine, req webSocketR } } -func webSocketSessionHandler(ws *websocket.Conn, sm *StateMachine, reqCh <-chan webSocketRequest) { +func webSocketSessionHandler(ws *websocket.Conn, sm *StateMachine, reqCh <-chan types.WebSocketRequest) { defer ws.Close() - subCh := make(chan FullState, 100) + subCh := make(chan types.FullState, 100) for { select { case state, ok := <-subCh: if !ok { return } - var result webSocketResponseState + var result types.WebSocketResponseState result.ResponseCode = http.StatusOK result.Type = "state" - result.State = state.state - result.Ratio = state.ratio - result.Lang = state.language + result.State = state.State + result.Ratio = state.Ratio + result.Lang = state.Language sendWebSocketResponse(ws, result) case req, ok := <-reqCh: if !ok { @@ -168,20 +144,20 @@ func webSocketHandler(sm *StateMachine, w http.ResponseWriter, r *http.Request) return } log.Println("Web(socket) client", ws.RemoteAddr(), "connected") - reqCh := make(chan webSocketRequest) + reqCh := make(chan types.WebSocketRequest) go webSocketSessionHandler(ws, sm, reqCh) defer close(reqCh) for { t, r, err := ws.NextReader() if err != nil { - log.Println("Web(socket) Client", ws.RemoteAddr(), "disconnected:", err) + log.Println("Web(socket) client", ws.RemoteAddr(), "disconnected:", err) return } switch t { case websocket.TextMessage: - var req webSocketRequest + var req types.WebSocketRequest dec := json.NewDecoder(r) dec.DisallowUnknownFields() if err := dec.Decode(&req); err != nil { |