update
This commit is contained in:
@@ -1,4 +1,4 @@
|
||||
from fastapi import FastAPI
|
||||
from fastapi import FastAPI, WebSocket, WebSocketDisconnect
|
||||
from fastapi.middleware.cors import CORSMiddleware
|
||||
|
||||
from database import SessionLocal, create_tables
|
||||
@@ -17,6 +17,32 @@ app.add_middleware(
|
||||
app.include_router(videos_router)
|
||||
app.include_router(profiles_router)
|
||||
|
||||
# --- WebSocket ---
|
||||
|
||||
connected_clients: set[WebSocket] = set()
|
||||
|
||||
|
||||
@app.websocket("/ws")
|
||||
async def websocket_endpoint(websocket: WebSocket):
|
||||
await websocket.accept()
|
||||
connected_clients.add(websocket)
|
||||
try:
|
||||
while True:
|
||||
await websocket.receive_text()
|
||||
except WebSocketDisconnect:
|
||||
connected_clients.discard(websocket)
|
||||
|
||||
|
||||
async def notify_clients(profile_ids: list[int]):
|
||||
message = ",".join(str(pid) for pid in profile_ids)
|
||||
for client in list(connected_clients):
|
||||
try:
|
||||
await client.send_text(message)
|
||||
except Exception:
|
||||
connected_clients.discard(client)
|
||||
|
||||
|
||||
# --- Startup ---
|
||||
|
||||
@app.on_event("startup")
|
||||
def startup():
|
||||
|
||||
Reference in New Issue
Block a user