docs/examples/websockets/stream_socket_access.py
raw| 1 | import asyncio |
| 2 | import time |
| 3 | from typing import Any, AsyncGenerator |
| 4 | |
| 5 | from litestar import Litestar, WebSocket, websocket_stream |
| 6 | |
| 7 | |
| 8 | @websocket_stream("/") |
| 9 | async def ping(socket: WebSocket) -> AsyncGenerator[dict[str, Any], None]: |
| 10 | while True: |
| 11 | yield {"time": time.time(), "client": socket.client} |
| 12 | await asyncio.sleep(0.5) |
| 13 | |
| 14 | |
| 15 | app = Litestar([ping]) |