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',
|
name: 'App',
|
||||||
components: {
|
components: {
|
||||||
HelloWorld
|
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>
|
</script>
|
||||||
|
|
|
||||||
|
|
@ -3,21 +3,7 @@
|
||||||
</template>
|
</template>
|
||||||
|
|
||||||
<script>
|
<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>
|
</script>
|
||||||
|
|
|
||||||
|
|
@ -53,8 +53,8 @@ func (img *image) SetPixel(message Message) int {
|
||||||
fmt.Printf("User %d tried accessing out of bounds \n", message.UserID)
|
fmt.Printf("User %d tried accessing out of bounds \n", message.UserID)
|
||||||
return 1
|
return 1
|
||||||
}
|
}
|
||||||
if message.Color >= 255 || message.Color < 0 {
|
if message.Color > 255 || message.Color < 0 {
|
||||||
fmt.Printf("User %d tried setting non existent color \n", message.UserID)
|
fmt.Printf("User %d tried setting non existent color %d \n", message.UserID, message.Color)
|
||||||
return 1
|
return 1
|
||||||
}
|
}
|
||||||
pos := uint32(message.X)*uint32(img.Width) + uint32(message.Y)
|
pos := uint32(message.X)*uint32(img.Width) + uint32(message.Y)
|
||||||
|
|
|
||||||
|
|
@ -33,7 +33,7 @@ async def sender(img):
|
||||||
message = pixel(
|
message = pixel(
|
||||||
x=rx,
|
x=rx,
|
||||||
y=ry,
|
y=ry,
|
||||||
color=int(img[rx][ry][0]*255),
|
color=int(sum(img[rx][ry])/3),
|
||||||
timestamp=int(time.time()),
|
timestamp=int(time.time()),
|
||||||
userid=1,
|
userid=1,
|
||||||
)
|
)
|
||||||
|
|
@ -42,7 +42,7 @@ async def sender(img):
|
||||||
if succ == "1":
|
if succ == "1":
|
||||||
print(message, "was not set")
|
print(message, "was not set")
|
||||||
|
|
||||||
await asyncio.sleep(0.05)
|
|
||||||
|
|
||||||
async def client():
|
async def client():
|
||||||
image = np.zeros(shape=[1000, 1000, 3], dtype=np.uint8)
|
image = np.zeros(shape=[1000, 1000, 3], dtype=np.uint8)
|
||||||
|
|
@ -57,14 +57,14 @@ async def client():
|
||||||
x = pixel(**json.loads(await websocket.recv()))
|
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] = ([y*255 for y in colors[x.color]])
|
||||||
image[x.x][x.y] = ((x.color, x.color, x.color))
|
image[x.x][x.y] = ((x.color, x.color, x.color))
|
||||||
if i% 5000 == 0:
|
#if i% 5000 == 0:
|
||||||
cv2.imshow("changes x", image)
|
# cv2.imshow("changes x", image)
|
||||||
cv2.waitKey(10) & 0XFF
|
# cv2.waitKey(10) & 0XFF
|
||||||
await websocket.send("1")
|
await websocket.send("1")
|
||||||
#print(i, x)
|
#print(i, x)
|
||||||
|
|
||||||
async def main():
|
async def main():
|
||||||
img=mpimg.imread('./logo.png')
|
img=mpimg.imread('./1.jpg')
|
||||||
coros = [sender(img) for _ in range(100)]
|
coros = [sender(img) for _ in range(100)]
|
||||||
coros.append(client())
|
coros.append(client())
|
||||||
returns = await asyncio.gather(*coros)
|
returns = await asyncio.gather(*coros)
|
||||||
|
|
@ -73,6 +73,6 @@ def asyncMain(x):
|
||||||
asyncio.get_event_loop().run_until_complete(main())
|
asyncio.get_event_loop().run_until_complete(main())
|
||||||
|
|
||||||
if __name__ == "__main__":
|
if __name__ == "__main__":
|
||||||
#with Pool(12) as p:
|
with Pool(12) as p:
|
||||||
# print(p.map(asyncMain, [() for _ in range(12)]))
|
print(p.map(asyncMain, [() for _ in range(12)]))
|
||||||
asyncMain(0)
|
#asyncMain(0)
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue