-
Notifications
You must be signed in to change notification settings - Fork 1
/
kazoo_websocket.py
71 lines (53 loc) · 1.72 KB
/
kazoo_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
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
import websockets as w
import asyncio
import json as j
from websockets import ConnectionClosed
from config import WEBSOCKET_Channel_Create, KAZOO_HOST
def jsonify(d):
"""
jsonify a message
:param d: a dict to turn into a json file
"""
jsonfied = j.dumps(d)
return jsonfied
# subscription message to crossbar with single binding
async def consumer(event):
"""
consumer code that prints the notifications from messages sent by the websocket connection
- does something with the messages sent by server
"""
event = j.loads(event)
print(j.dumps(event, indent=2, sort_keys=True))
data = event['data']
call_vars = data.get('custom_channel_vars')
owner_id = call_vars.get('owner_id')
interaction_id = call_vars.get('call_interaction_id')
print(data)
print('-' * 1000)
print(owner_id)
print(interaction_id)
# item_type = data.get('type')
# item_id = data.get('id')
# account_id = data.get('account_id')
# print(f'item_type: {item_type}, item_id: {item_id}, account_id: {account_id}')
async def hello():
"""
Start connection to blackhole over 5555
:return:
"""
print('trying to connect')
async with w.connect(f"ws://{KAZOO_HOST}:5555") as ws:
print('connected')
await ws.send(jsonify(WEBSOCKET_Channel_Create))
try:
response = await ws.recv()
print(response)
except ConnectionClosed:
print('closed connection')
"""
consumer code that handles the messages sent by the server
"""
async for messages in ws:
await consumer(messages)
if __name__ == '__main__':
asyncio.get_event_loop().run_until_complete(hello())