update
This commit is contained in:
0
backend/notify/__init__.py
Normal file
0
backend/notify/__init__.py
Normal file
24
backend/notify/notify_clients.py
Normal file
24
backend/notify/notify_clients.py
Normal file
@@ -0,0 +1,24 @@
|
||||
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)
|
||||
Reference in New Issue
Block a user