-
Notifications
You must be signed in to change notification settings - Fork 0
/
websocket.py
40 lines (34 loc) · 1.1 KB
/
websocket.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
import main
import simulate
import json
import asyncio
import websockets
from dotenv import load_dotenv
load_dotenv()
async def connect():
uri = "ws://connect.websocket.in/consumo?room_id=" + 1234
async with websockets.connect(uri) as websocket:
while True:
print('waiting for messages...')
request = await websocket.recv()
requestJson = json.loads(request)
if 'command' in requestJson:
# TODO fix bug; simulate.main() causing exception in asyncio
if(requestJson['command'] == 'start' and requestJson['type'] == 'simulate'):
print('starting simulation')
simulate.main()
if(requestJson['command'] == 'start' and requestJson['type'] == 'main'):
print('starting main')
main.main()
else:
print(requestJson)
loop = asyncio.get_event_loop()
try:
loop.create_task(connect())
loop.run_forever()
except KeyboardInterrupt:
pass
finally:
print("Closing Loop")
loop.stop()
loop.close()