-
Notifications
You must be signed in to change notification settings - Fork 1
/
manager_websocket.py
51 lines (43 loc) · 1.46 KB
/
manager_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
41
42
43
44
45
46
47
48
49
50
51
import json
import asyncio
import websockets
from logger import log_info, log_error
WEBSOCKET_SERVER_LOCAL = 'ws://localhost:5000'
COMM_ROOM_TIMEOUT = 2
COMM_ERR_GENERIC = -10
COMM_ERR_TIMEOUT = -20
async def wait_room_reply(ws):
response = await ws.recv() # wait for room confirmation
return response
async def event(message):
'''
communication to the rooms
- create a connection to the broker
- wait for broker confirm
- wait for a single room confirm (timeout)
- close the connection
'''
async with websockets.connect(WEBSOCKET_SERVER_LOCAL) as ws:
message = json.dumps(message)
print(f'send message to broker: {message}')
await ws.send(message)
# print('broker confirmed')
res = await ws.recv() # await reply from broker
print(f'message from broker: {res}')
try:
res = await asyncio.wait_for(wait_room_reply(ws), timeout=COMM_ROOM_TIMEOUT) # await reply from room
print(f'message from room: {res}')
except asyncio.exceptions.TimeoutError as exc:
raise exc
print('exit')
def send_event(event_name, data):
_d = {'event': event_name, 'data': data, 'sender': 'manager', 'timestamp': ''}
try:
asyncio.get_event_loop().run_until_complete(event(_d))
except asyncio.exceptions.TimeoutError as exc:
print(str(exc))
log_error('room timeout! Can\'t communicate with the room')
return COMM_ERR_TIMEOUT
except Exception as exc:
log_error(str(exc))
return COMM_ERR_GENERIC