-
Notifications
You must be signed in to change notification settings - Fork 1
/
service.py
84 lines (61 loc) · 2.09 KB
/
service.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
72
73
74
75
76
77
78
79
80
81
82
83
84
#!/usr/bin/env python
# WS server example that synchronizes state across clients
import asyncio
import json
import logging
import websockets
logging.basicConfig()
DATA = dict()
USERS = set()
def get_stats():
if DATA:
stats = [0] * 6
for user in USERS:
value = DATA[user]
stats[value] += 1
return json.dumps({"type": "state", "states" : [stats[1], stats[2], stats[3], stats[4], stats[5]]})
def users_count():
return json.dumps({"type": "users", "count": len(USERS)})
async def notify_state():
if DATA: # asyncio.wait doesn't accept an empty list
message = get_stats()
await asyncio.wait([user.send(message) for user in USERS])
async def notify_users():
if DATA: # asyncio.wait doesn't accept an empty list
message = users_count()
await asyncio.wait([user.send(message) for user in USERS])
async def register(websocket):
USERS.add(websocket)
DATA[websocket] = 4
await notify_users()
await notify_state()
async def unregister(websocket):
USERS.remove(websocket)
DATA.pop(websocket)
await notify_users()
await notify_state()
async def alert():
message = json.dumps({"type": "alert"})
await asyncio.wait([user.send(message) for user in USERS])
async def counter(websocket, path):
# register(websocket) sends user_event() to websocket
await register(websocket)
try:
await websocket.send(get_stats())
async for message in websocket:
data = json.loads(message)
if data["type"] == "update":
number = int(data["value"])
if number >= 0 and number <= 5:
DATA[websocket] = number
await notify_state()
else:
logging.error(data)
elif data["type"] == "alert":
await alert()
finally:
await unregister(websocket)
if __name__ == '__main__':
start_server = websockets.serve(counter, "", 3015)
asyncio.get_event_loop().run_until_complete(start_server)
asyncio.get_event_loop().run_forever()