Spaces:
Sleeping
Sleeping
Download app.py from Subham9126/websocket: direct link, hf CLI and curl.
- Browser
- Download file 534 Bytes
-
https://huggingface.co/spaces/Subham9126/websocket/resolve/main/app.py
- Command line
-
hf download hf://spaces/Subham9126/websocket/app.py
-
curl -L -o app.py https://huggingface.co/spaces/Subham9126/websocket/resolve/main/app.py
534 Bytes
| import asyncio | |
| import random | |
| from fastapi import FastAPI, WebSocket, WebSocketDisconnect | |
| app = FastAPI() | |
| def random_8_digit(): | |
| return random.randint(10_000_000, 99_999_999) | |
| async def websocket_endpoint(websocket: WebSocket): | |
| await websocket.accept() | |
| try: | |
| while True: | |
| number = random_8_digit() | |
| await websocket.send_json({"number": number}) | |
| await asyncio.sleep(1) # stream every second | |
| except WebSocketDisconnect: | |
| print("Client disconnected") |