diff options
-rw-r--r-- | src/rhimportd/ctrlWebSocket.go | 35 | ||||
-rw-r--r-- | web-static/socket.html | 17 |
2 files changed, 45 insertions, 7 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) } } } diff --git a/web-static/socket.html b/web-static/socket.html index fda00e9..3737c5f 100644 --- a/web-static/socket.html +++ b/web-static/socket.html @@ -74,6 +74,10 @@ } this.sock.onopen = this.sock_onopen.bind(this); + this.sendbinmsg = function(data) { + this.sock.send(data); + } + this.cancel = function() { this.sock.send(JSON.stringify({COMMAND: "cancel"})); } @@ -100,6 +104,14 @@ s = new Session(req); } + function sendbinmsg() { + var byteArray = new Uint8Array(100); + for (var x = 0; x < byteArray.length; x++){ + byteArray[x] = x + } + s.sendbinmsg(new Blob([byteArray], {type: "application/octet-stream"})); + } + function cancel() { s.cancel(); } @@ -112,6 +124,7 @@ function buttonsIdle() { $('#buttonrun').removeAttr('disabled') $('#buttonreconnect').removeAttr('disabled') + $('#buttonbinmsg').attr('disabled','disabled') $('#buttondetach').attr('disabled','disabled') $('#buttoncancel').attr('disabled','disabled') } @@ -119,6 +132,7 @@ function buttonsRunning() { $('#buttonrun').attr('disabled','disabled') $('#buttonreconnect').attr('disabled','disabled') + $('#buttonbinmsg').removeAttr('disabled') $('#buttondetach').removeAttr('disabled') $('#buttoncancel').removeAttr('disabled') } @@ -127,7 +141,7 @@ $('#sessionid').val(""); buttonsIdle(); } -</script> + </script> </head> <body onload="init()"> @@ -149,6 +163,7 @@ <input id="source" type="text" size="30" value="fake://10000"> <input id="refid" type="text" size="15" value="test-reference-id"> <button id="buttonrun" onclick="run()">start</button> + <button id="buttonbinmsg" onclick="sendbinmsg()">send binary message</button> <button id="buttondetach" onclick="detach()">detach</button> <input id="sessionid" type="text" size="45"> <button id="buttonreconnect" onclick="reconnect()">reconnect</button> |