Subham9126 commited on
Commit
f2ed7ab
·
verified ·
1 Parent(s): cadca6c

Create app.py

Browse files
Files changed (1) hide show
  1. app.py +19 -0
app.py ADDED
@@ -0,0 +1,19 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import asyncio
2
+ import random
3
+ from fastapi import FastAPI, WebSocket, WebSocketDisconnect
4
+
5
+ app = FastAPI()
6
+
7
+ def random_8_digit():
8
+ return random.randint(10_000_000, 99_999_999)
9
+
10
+ @app.websocket("/ws")
11
+ async def websocket_endpoint(websocket: WebSocket):
12
+ await websocket.accept()
13
+ try:
14
+ while True:
15
+ number = random_8_digit()
16
+ await websocket.send_json({"number": number})
17
+ await asyncio.sleep(1) # stream every second
18
+ except WebSocketDisconnect:
19
+ print("Client disconnected")