-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathclient.py
60 lines (46 loc) · 1.31 KB
/
client.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
import aioconsole
import websockets
import asyncio
import json
IP = "localhost:8765"
player_id = 0
ws = websockets.connect("ws://" + IP)
connected = False
async def connect():
global ws
global connected
async with websockets.connect("ws://localhost:8765") as wsx:
ws = wsx
connected = True
while True:
await asyncio.sleep(10)
async def recieve_data_from_server():
while not connected:
await asyncio.sleep(1)
global ws
global player_id
while True:
async for message in ws:
try:
data = json.loads(message)
if data["action"] == "print":
print(data["data"])
elif data["action"] == "id":
player_id = data["id"]
except Exception as e:
print(e)
print(message)
async def send_data_to_server():
while not connected:
await asyncio.sleep(1)
global ws
global player_id
while True:
cmd = await aioconsole.ainput()
data = {"action": cmd, "id": player_id}
print(data)
await ws.send(json.dumps(data))
loop = asyncio.get_event_loop()
async def run_stuff():
await asyncio.gather(connect(), recieve_data_from_server(), send_data_to_server())
asyncio.run(run_stuff())