Spaces:
Sleeping
Sleeping
File size: 534 Bytes
f2ed7ab | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 | 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)
@app.websocket("/ws")
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") |