From 71de69ce36191fa255fd38e9f38bd3132aff72a4 Mon Sep 17 00:00:00 2001 From: Christian Pointner Date: Fri, 24 Jun 2016 02:03:34 +0200 Subject: receiving binary messages works 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(); } - + @@ -149,6 +163,7 @@ + -- cgit v0.10.2