Hello people,
I’m stuck with websockets and image processing, I want to send an image from the client side using either native Websocket or SocketIO and then receive that image in the server and apply it some image filters using this package blind.
I have the following code in the client side:
const file = document.querySelector('#file');
const ws = new WebSocket(`ws://${window.location.host}/ws/upload`);
function handleFile(e) {
let file = e.target.files[0];
if (!file) return;
ws.send(file);
}
}
file.addEventListener('change', handleFile);
ws.addEventListener('message', e => {
console.log(e);
});
And using gorilla websockets I have the following:
// Omitting some code
r.HandleFunc("/ws/upload", upload)
func upload(w http.ResponseWriter, r *http.Request) {
ws, err := upgrader.Upgrade(w, r, w.Header())
if err != nil {
logrus.Fatal(err.Error())
return
}
defer ws.Close()
for {
msgType, p, err := ws.ReadMessage()
if err != nil {
logrus.Fatal(err.Error())
return
}
if err := ws.WriteMessage(msg, p); err != nil {
logrus.Fatal(err.Error())
return
}
}
}
The ws.ReadMessage()
gives me the message type msgType
, p
which is the data a slice of bytes (which is also the image), and an error
. My problem is that I don’t know how to convert that []byte
to an image and then apply it some filters. Any help is welcome and thanks beforehand .