-
Notifications
You must be signed in to change notification settings - Fork 0
/
server.py
35 lines (27 loc) · 957 Bytes
/
server.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
import asyncio
import websockets
import json
connected = set()
config = {}
async def server(websocket, path):
# Register.
connected.add(websocket)
try:
async for message in websocket:
message = json.loads(message)
if(message["operation"] == "update"):
config[message["key"]] = message["value"]
if(message["operation"] == "remove"):
if(message["key"] in config):
config.pop(message["key"])
if(message["operation"] == "get"):
await websocket.send(json.dumps(config))
else:
for conn in connected:
await conn.send(json.dumps(config))
finally:
# Unregister.
connected.remove(websocket)
start_server = websockets.serve(server, "localhost", 5000)
asyncio.get_event_loop().run_until_complete(start_server)
asyncio.get_event_loop().run_forever()