This repository was archived by the owner on Sep 14, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathserver.py
executable file
·57 lines (49 loc) · 1.69 KB
/
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
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
#!/usr/bin/env python3
"Websocket server that synchronizes state across clients"
import asyncio
import json
import sys
import websockets
from collections import defaultdict
CLIENTS = defaultdict(set)
STATE = defaultdict(dict)
async def distributor(websocket, path):
CLIENTS[path].add(websocket)
await websocket.send(json.dumps(STATE[path]))
print('%s:%s connected' % websocket.remote_address, path)
try:
async for message in websocket:
try:
STATE[path].update(json.loads(message))
except Exception:
print('Message is not valid JSON', message)
if len(CLIENTS[path]) > 1: # asyncio.wait doesn't accept an empty list
await asyncio.wait([client.send(message) for client in CLIENTS[path] if client != websocket])
print('%s:%s' % websocket.remote_address, message)
finally:
CLIENTS[path].remove(websocket)
if not CLIENTS[path]:
del CLIENTS[path]
print('%s:%s disconnected' % websocket.remote_address)
def help():
print('''Websocket sync server
Relays each message to all clients.
Usage:', sys.argv[0], '[host [port]]''')
if __name__ == '__main__':
if '-h' in sys.argv or '--help' in sys.argv:
help()
else:
host = 'localhost'
port = 8000
try:
host = sys.argv[1]
port = int(sys.argv[2])
except (IndexError, ValueError):
pass
try:
asyncio.get_event_loop().run_until_complete(
websockets.serve(distributor, host, port))
asyncio.get_event_loop().run_forever()
except Exception:
help()
raise