-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfastapi_server.py
71 lines (50 loc) · 2.17 KB
/
fastapi_server.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
from typing import List
import uvicorn
from fastapi import FastAPI, WebSocket, WebSocketDisconnect
from fastapi.requests import Request
class ConnectionManager:
def __init__(self):
self.active_connections: List[WebSocket] = []
async def connect(self, websocket: WebSocket):
await websocket.accept()
self.active_connections.append(websocket)
def disconnect(self, websocket: WebSocket):
self.active_connections.remove(websocket)
async def send_personal_message(self, message: str, websocket: WebSocket):
await websocket.send_text(message)
async def broadcast(self, message: str):
for connection in self.active_connections:
await connection.send_text(message)
manager = ConnectionManager()
app = FastAPI()
@app.post('/change_page')
async def change_page(request: Request):
data = await request.json()
# Here is where I need to send pageNum via the websocket, so that it reaches the client
@app.get("/")
async def get():
return 'Welcome to Overseer, from our fastapi server.'
@app.websocket("/ws/{client_id}")
async def websocket_endpoint(websocket: WebSocket, client_id: str):
await manager.connect(websocket)
try:
while True:
try:
data = await websocket.receive()
except RuntimeError:
continue
data = data.get('text')
if data is None:
# manager.disconnect(websocket)
# await manager.broadcast(f"{client_id} left the chat")
break
elif 'page_number_' in data:
# await manager.send_personal_message(f"Hello, {client_id} from Overseer's fastapi websocket server.", websocket=websocket)
await manager.broadcast(f"Hello, from Overseer's fastapi websocket server.")
# await manager.broadcast(f"{client_id} left the chat")
# manager.disconnect(websocket)
except WebSocketDisconnect:
manager.disconnect(websocket)
await manager.broadcast(f"{client_id} left the chat")
if __name__ == "__main__":
uvicorn.run("fastapi_server:app", host="127.0.0.1", port=8000, log_level="info")