r_place/python/clients.py

42 lines
856 B
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
import websockets
@dataclass
class pixel:
x: int
y: int
color: int
timestamp: int
userid: int
async def main():
async with websockets.connect("ws://localhost:8080/") as websocket:
2022-05-28 13:28:14 +00:00
for _ in range(10):
message = pixel(
2022-05-28 13:28:14 +00:00
x=random.randint(0, 9),
y=random.randint(0, 9),
color=random.randint(0,15),
timestamp=int(time.time()),
userid=1,
)
print(message)
await websocket.send(json.dumps(message.__dict__))
while True:
x = await websocket.recv()
print(x)
2022-05-26 12:14:36 +00:00
if __name__ == "__main__":
asyncio.get_event_loop().run_until_complete(main())