| 1 |
#!/usr/bin/env python |
| 2 |
|
| 3 |
import asyncio |
| 4 |
import datetime |
| 5 |
import random |
| 6 |
|
| 7 |
from websockets.asyncio.server import broadcast, serve |
| 8 |
|
| 9 |
async def noop(websocket): |
| 10 |
await websocket.wait_closed() |
| 11 |
|
| 12 |
async def show_time(server): |
| 13 |
while True: |
| 14 |
message = datetime.datetime.now(tz=datetime.timezone.utc).isoformat() |
| 15 |
broadcast(server.connections, message) |
| 16 |
await asyncio.sleep(random.random() * 2 + 1) |
| 17 |
|
| 18 |
async def main(): |
| 19 |
async with serve(noop, "localhost", 5678) as server: |
| 20 |
await show_time(server) |
| 21 |
|
| 22 |
if __name__ == "__main__": |
| 23 |
asyncio.run(main()) |