25 lines
748 B
Python
25 lines
748 B
Python
from fastapi import FastAPI, WebSocket, WebSocketDisconnect
|
|
|
|
connected_clients: set[WebSocket] = set()
|
|
|
|
|
|
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)
|
|
|
|
|
|
def register_websocket(app: FastAPI):
|
|
@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)
|