blob: 08e87f5931a44fc0d771a682be503a6df783b094 (
plain)
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
|
#!/usr/bin/env python
import asyncio
import datetime
import random
import websockets
CONNECTIONS = set()
async def register(websocket):
CONNECTIONS.add(websocket)
try:
await websocket.wait_closed()
finally:
CONNECTIONS.remove(websocket)
async def show_time():
while True:
message = datetime.datetime.utcnow().isoformat() + "Z"
websockets.broadcast(CONNECTIONS, message)
await asyncio.sleep(random.random() * 2 + 1)
async def main():
async with websockets.serve(register, "localhost", 5678):
await show_time()
if __name__ == "__main__":
asyncio.run(main())
|