2022-05-26 12:14:36 +00:00
|
|
|
#!/usr/bin/env python
|
|
|
|
|
|
|
|
|
|
import asyncio
|
|
|
|
|
from dataclasses import dataclass
|
|
|
|
|
import datetime
|
|
|
|
|
|
|
|
|
|
import json
|
2022-05-27 22:30:15 +00:00
|
|
|
import random
|
2022-05-26 12:14:36 +00:00
|
|
|
import time
|
|
|
|
|
|
|
|
|
|
import websockets
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
@dataclass
|
|
|
|
|
class pixel:
|
|
|
|
|
x: int
|
|
|
|
|
y: int
|
|
|
|
|
color: int
|
|
|
|
|
timestamp: int
|
|
|
|
|
userid: int
|
|
|
|
|
|
2022-05-28 14:12:56 +00:00
|
|
|
async def sender():
|
2022-05-27 22:30:15 +00:00
|
|
|
async with websockets.connect("ws://localhost:8080/") as websocket:
|
2022-05-28 14:12:56 +00:00
|
|
|
while True:
|
2022-05-27 22:30:15 +00:00
|
|
|
message = pixel(
|
2022-05-28 13:28:14 +00:00
|
|
|
x=random.randint(0, 9),
|
|
|
|
|
y=random.randint(0, 9),
|
2022-05-27 22:30:15 +00:00
|
|
|
color=random.randint(0,15),
|
|
|
|
|
timestamp=int(time.time()),
|
|
|
|
|
userid=1,
|
|
|
|
|
)
|
|
|
|
|
await websocket.send(json.dumps(message.__dict__))
|
2022-05-28 14:12:56 +00:00
|
|
|
await asyncio.sleep(0.1)
|
|
|
|
|
|
|
|
|
|
async def client():
|
|
|
|
|
async with websockets.connect("ws://localhost:8080/") as websocket:
|
|
|
|
|
i= 0
|
2022-05-27 22:30:15 +00:00
|
|
|
while True:
|
2022-05-28 14:12:56 +00:00
|
|
|
i+=1
|
2022-05-27 22:30:15 +00:00
|
|
|
x = await websocket.recv()
|
2022-05-28 14:12:56 +00:00
|
|
|
print(i, pixel(**json.loads(x)))
|
2022-05-26 12:14:36 +00:00
|
|
|
|
2022-05-28 14:12:56 +00:00
|
|
|
async def main():
|
|
|
|
|
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())
|