r_place/python/clients.py

64 lines
1.6 KiB
Python
Raw Normal View History

2022-05-26 12:14:36 +00:00
#!/usr/bin/env python
import asyncio
from dataclasses import dataclass
import datetime
import json
import random
2022-05-26 12:14:36 +00:00
import time
2022-05-29 10:08:56 +00:00
import matplotlib
from matplotlib import pyplot as plt
import numpy as np
2022-05-26 12:14:36 +00:00
import websockets
2022-05-29 10:08:56 +00:00
import cv2
2022-05-26 12:14:36 +00:00
@dataclass
class pixel:
x: int
y: int
color: int
timestamp: int
userid: int
async def sender():
async with websockets.connect("ws://localhost:8080/set") as websocket:
while True:
message = pixel(
2022-05-29 10:08:56 +00:00
x=random.randint(0, 99),
y=random.randint(0, 99),
color=random.randint(0,11),
timestamp=int(time.time()),
userid=1,
)
await websocket.send(json.dumps(message.__dict__))
await asyncio.sleep(0.1)
async def client():
2022-05-29 10:08:56 +00:00
image = np.zeros(shape=[100, 100, 3], dtype=np.uint8)
colors = []
for name, hex in matplotlib.colors.cnames.items():
colors.append(matplotlib.colors.to_rgb(hex))
async with websockets.connect("ws://localhost:8080/get") as websocket:
i= 0
while True:
i+=1
2022-05-29 10:08:56 +00:00
x = pixel(**json.loads(await websocket.recv()))
image[x.x][x.y] = ([y*255 for y in colors[x.color]])
if i% 1000 == 0:
cv2.imshow("changes x", image)
cv2.waitKey(10) & 0XFF
print(i, x)
2022-05-26 12:14:36 +00:00
async def main():
2022-05-29 10:08:56 +00:00
coros = [sender() for _ in range(100)]
coros.append(client())
returns = await asyncio.gather(*coros)
2022-05-26 12:14:36 +00:00
if __name__ == "__main__":
asyncio.get_event_loop().run_until_complete(main())