js connection to WS

This commit is contained in:
Askill 2022-06-05 11:01:18 +02:00
parent f587f8278f
commit dc51bc6098
3 changed files with 16 additions and 7 deletions

View File

@ -2,8 +2,8 @@
<canvas id="main_canvas" height="1000" width="1000"> </canvas>
</template>
<script>
const wsConnection = new WebSocket('ws:localhost:8080/get', 'json');
<script>
var wsConnection = new WebSocket('ws://localhost:8080/get');
wsConnection.onopen = (e) => {
console.log(`wsConnection open to 127.0.0.1:8080`, e);
};
@ -11,8 +11,15 @@ wsConnection.onerror = (e) => {
console.error(`wsConnection error `, e);
};
wsConnection.onmessage = (e) => {
console.log(JSON.parse(e.data));
//var canvas = document.getElementById("main_canvas");
//var ctx = canvas.getContext("2d");
let data = JSON.parse(e.data)
console.log(data);
//ctx.fillStyle = "rgba("+data["color"][0]+","+data["color"][1]+","+data["color"][2]+","+(255)+")";
//ctx.fillRect(data["x"], data["y"], 1, 1);
};
</script>
<!-- Add "scoped" attribute to limit CSS to this component only -->

View File

@ -62,12 +62,14 @@ func get(w http.ResponseWriter, r *http.Request) {
msg := Message{X: uint32(x), Y: uint32(y), Timestamp: pix.Pixel.Timestamp, UserID: pix.Pixel.UserID, Color: pix.Pixel.Color}
marshalMsg, err := json.Marshal(msg)
if err != nil {
log.Println("error while writing image", err)
log.Println("error while marshalling image", err)
break
}
err = c.WriteMessage(1, marshalMsg)
_, msg2, _ := c.ReadMessage()
_ = msg2
if err != nil {
log.Println("error while writing image", err)
break
}
}
}
if err := c.WriteMessage(websocket.PingMessage, []byte{}); err != nil {

View File

@ -57,7 +57,7 @@ async def client():
x = pixel(**json.loads(await websocket.recv()))
#image[x.x][x.y] = ([y*255 for y in colors[x.color]])
image[x.x][x.y] = ((x.color, x.color, x.color))
if i% 500000 == 0:
if i% 5000 == 0:
cv2.imshow("changes x", image)
cv2.waitKey(10) & 0XFF
await websocket.send("1")