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
|
#!/usr/bin/env python
import asyncio
import json
import logging
from websockets.asyncio.server import broadcast, serve
logging.basicConfig()
USERS = set()
VALUE = 0
def users_event():
return json.dumps({"type": "users", "count": len(USERS)})
def value_event():
return json.dumps({"type": "value", "value": VALUE})
async def counter(websocket):
global USERS, VALUE
try:
# Register user
USERS.add(websocket)
broadcast(USERS, users_event())
# Send current state to user
await websocket.send(value_event())
# Manage state changes
async for message in websocket:
event = json.loads(message)
if event["action"] == "minus":
VALUE -= 1
broadcast(USERS, value_event())
elif event["action"] == "plus":
VALUE += 1
broadcast(USERS, value_event())
else:
logging.error("unsupported event: %s", event)
finally:
# Unregister user
USERS.remove(websocket)
broadcast(USERS, users_event())
async def main():
async with serve(counter, "localhost", 6789) as server:
await server.serve_forever()
if __name__ == "__main__":
asyncio.run(main())
|