-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.py
More file actions
25 lines (19 loc) · 815 Bytes
/
Copy pathmain.py
File metadata and controls
25 lines (19 loc) · 815 Bytes
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
from fastapi import FastAPI, WebSocket, WebSocketDisconnect
from starlette.responses import FileResponse
from utils.websocket_utils import ConnectionManager
app = FastAPI()
@app.get("/")
async def index():
return FileResponse("static/index.html")
@app.websocket("/ws/{client_id}")
async def websocket(web_socket: WebSocket, client_id: int):
manager = ConnectionManager()
await manager.connect(web_socket)
try:
while True:
data = await web_socket.receive_text()
await manager.send_a_message(f"you wrote {data}",web_socket)
await manager.broadcast_message(f"Client {client_id} says {data}")
except WebSocketDisconnect:
manager.disconnect(web_socket)
await manager.broadcast_message(f"Client {client_id} has left the chat!")