mirror of https://github.com/Askill/r_place.git
streaming to JS fe works
This commit is contained in:
parent
dc51bc6098
commit
e9a00b3027
|
|
@ -9,6 +9,23 @@ export default {
|
|||
name: 'App',
|
||||
components: {
|
||||
HelloWorld
|
||||
},
|
||||
created(){
|
||||
var wsConnection = new WebSocket('ws://localhost:8080/get');
|
||||
wsConnection.onopen = (e) => {
|
||||
console.log(`wsConnection open to 127.0.0.1:8080`, e);
|
||||
};
|
||||
wsConnection.onerror = (e) => {
|
||||
console.error(`wsConnection error `, e);
|
||||
};
|
||||
wsConnection.onmessage = (e) => {
|
||||
var canvas = document.getElementById("main_canvas");
|
||||
var ctx = canvas.getContext("2d");
|
||||
let data = JSON.parse(e.data)
|
||||
|
||||
ctx.fillStyle = "rgba("+data["color"]+","+data["color"]+","+data["color"]+","+(255)+")";
|
||||
ctx.fillRect(data["y"], data["x"], 1, 1);
|
||||
};
|
||||
}
|
||||
}
|
||||
</script>
|
||||
|
|
|
|||
|
|
@ -3,21 +3,7 @@
|
|||
</template>
|
||||
|
||||
<script>
|
||||
var wsConnection = new WebSocket('ws://localhost:8080/get');
|
||||
wsConnection.onopen = (e) => {
|
||||
console.log(`wsConnection open to 127.0.0.1:8080`, e);
|
||||
};
|
||||
wsConnection.onerror = (e) => {
|
||||
console.error(`wsConnection error `, e);
|
||||
};
|
||||
wsConnection.onmessage = (e) => {
|
||||
//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>
|
||||
|
|
|
|||
|
|
@ -53,8 +53,8 @@ func (img *image) SetPixel(message Message) int {
|
|||
fmt.Printf("User %d tried accessing out of bounds \n", message.UserID)
|
||||
return 1
|
||||
}
|
||||
if message.Color >= 255 || message.Color < 0 {
|
||||
fmt.Printf("User %d tried setting non existent color \n", message.UserID)
|
||||
if message.Color > 255 || message.Color < 0 {
|
||||
fmt.Printf("User %d tried setting non existent color %d \n", message.UserID, message.Color)
|
||||
return 1
|
||||
}
|
||||
pos := uint32(message.X)*uint32(img.Width) + uint32(message.Y)
|
||||
|
|
|
|||
|
|
@ -33,7 +33,7 @@ async def sender(img):
|
|||
message = pixel(
|
||||
x=rx,
|
||||
y=ry,
|
||||
color=int(img[rx][ry][0]*255),
|
||||
color=int(sum(img[rx][ry])/3),
|
||||
timestamp=int(time.time()),
|
||||
userid=1,
|
||||
)
|
||||
|
|
@ -42,7 +42,7 @@ async def sender(img):
|
|||
if succ == "1":
|
||||
print(message, "was not set")
|
||||
|
||||
await asyncio.sleep(0.05)
|
||||
|
||||
|
||||
async def client():
|
||||
image = np.zeros(shape=[1000, 1000, 3], dtype=np.uint8)
|
||||
|
|
@ -57,14 +57,14 @@ 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% 5000 == 0:
|
||||
cv2.imshow("changes x", image)
|
||||
cv2.waitKey(10) & 0XFF
|
||||
#if i% 5000 == 0:
|
||||
# cv2.imshow("changes x", image)
|
||||
# cv2.waitKey(10) & 0XFF
|
||||
await websocket.send("1")
|
||||
#print(i, x)
|
||||
|
||||
async def main():
|
||||
img=mpimg.imread('./logo.png')
|
||||
img=mpimg.imread('./1.jpg')
|
||||
coros = [sender(img) for _ in range(100)]
|
||||
coros.append(client())
|
||||
returns = await asyncio.gather(*coros)
|
||||
|
|
@ -73,6 +73,6 @@ def asyncMain(x):
|
|||
asyncio.get_event_loop().run_until_complete(main())
|
||||
|
||||
if __name__ == "__main__":
|
||||
#with Pool(12) as p:
|
||||
# print(p.map(asyncMain, [() for _ in range(12)]))
|
||||
asyncMain(0)
|
||||
with Pool(12) as p:
|
||||
print(p.map(asyncMain, [() for _ in range(12)]))
|
||||
#asyncMain(0)
|
||||
|
|
|
|||
Loading…
Reference in New Issue