diff options
author | Christian Pointner <equinox@helsinki.at> | 2016-06-24 00:03:34 (GMT) |
---|---|---|
committer | Christian Pointner <equinox@helsinki.at> | 2016-06-24 00:03:34 (GMT) |
commit | 71de69ce36191fa255fd38e9f38bd3132aff72a4 (patch) | |
tree | 53ff9883398401b3620ed5a1b95af87e7d8052f8 /src | |
parent | 65a9d2c8d9eb7d4880ee1527de54fa212035af10 (diff) |
receiving binary messages works
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) } } } |