diff options
Diffstat (limited to 'src')
-rw-r--r-- | src/rhimportd/ctrlWebSocket.go | 35 |
1 files changed, 29 insertions, 6 deletions
diff --git a/src/rhimportd/ctrlWebSocket.go b/src/rhimportd/ctrlWebSocket.go index be7dcc0..d71f567 100644 --- a/src/rhimportd/ctrlWebSocket.go +++ b/src/rhimportd/ctrlWebSocket.go @@ -27,9 +27,12 @@ package main import ( "code.helsinki.at/rhrd-go/rddb" "code.helsinki.at/rhrd-go/rhimport" + "encoding/json" "fmt" "github.com/gorilla/websocket" "html" + "io" + "io/ioutil" "math" "net/http" "time" @@ -320,14 +323,34 @@ func webSocketHandler(conf *rhimport.Config, db *rddb.DBChan, sessions *rhimport defer close(reqchan) for { - reqdata := newWebSocketRequestData(conf) - if err := ws.ReadJSON(&reqdata); err != nil { + t, r, err := ws.NextReader() + if err != nil { rhdl.Println("WebSocket Client", ws.RemoteAddr(), "disconnected:", err) - sendWebSocketErrorResponse(ws, http.StatusBadRequest, err.Error()) + // sendWebSocketErrorResponse(ws, http.StatusBadRequest, err.Error()) return - } else { - // rhdl.Printf("Websocket Client %s got: %+v", ws.RemoteAddr(), reqdata) - reqchan <- *reqdata + } + + switch t { + case websocket.TextMessage: + reqdata := newWebSocketRequestData(conf) + if err := json.NewDecoder(r).Decode(&reqdata); err != nil { + if err == io.EOF { + err = io.ErrUnexpectedEOF + } + rhdl.Println("WebSocket Client", ws.RemoteAddr(), "disconnected:", err) + // sendWebSocketErrorResponse(ws, http.StatusBadRequest, err.Error()) + return + } else { + // rhdl.Printf("Websocket Client %s got: %+v", ws.RemoteAddr(), reqdata) + reqchan <- *reqdata + } + case websocket.BinaryMessage: + len, err := io.Copy(ioutil.Discard, r) + if err != nil { + rhdl.Println("WebSocket Client", ws.RemoteAddr(), "disconnected:", err) + return + } + rhdl.Printf("WebSocket Client %s: got binary message (%d bytes)", ws.RemoteAddr(), len) } } } |