-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathexample.py
54 lines (44 loc) · 1.4 KB
/
example.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
import json
import random
import sys
import websocket
request_id = f'random-req-id-{random.randint(1, 100000000)}'
sub_id = f'random-sub-id-{random.randint(1, 100000000)}'
def on_message(ws, raw_msg):
msg = json.loads(raw_msg)
if msg['type'] == 'response':
if not msg['success']:
raise Exception('subscribe request failure')
elif msg['type'] == 'event' and msg['success']:
data = msg['data']
print()
print(json.dumps(data, sort_keys=True, indent=4))
def on_error(ws, error):
print("socket error", error)
def on_close(ws, status_code, msg):
print("socket closed", status_code, msg)
def on_open(ws):
ws.send(json.dumps({
"type": "request",
"uid": request_id,
"data": {
"method": "subscribe",
"arg": {
"event": "nearby", # watching, nearby, groups, etc...
"subId": sub_id
}
}
}))
if __name__ == "__main__":
#websocket.enableTrace(True)
if len(sys.argv) < 2:
host = "ws://localhost:1080/api/ws/events"
else:
host = sys.argv[1]
print("Connecting to:", host)
ws = websocket.WebSocketApp(host,
on_open = on_open,
on_message=on_message,
on_error=on_error,
on_close=on_close)
ws.run_forever()