update
This commit is contained in:
@@ -1,24 +1,26 @@
|
||||
from fastapi import FastAPI, WebSocket, WebSocketDisconnect
|
||||
|
||||
connected_clients: set[WebSocket] = set()
|
||||
connectedClients: 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):
|
||||
async def notifyClients(profileId: int | None):
|
||||
if not profileId:
|
||||
profileId = 1
|
||||
message = str(profileId)
|
||||
for client in list(connectedClients):
|
||||
try:
|
||||
await client.send_text(message)
|
||||
except Exception:
|
||||
connected_clients.discard(client)
|
||||
connectedClients.discard(client)
|
||||
|
||||
|
||||
def register_websocket(app: FastAPI):
|
||||
def registerWebsocket(app: FastAPI):
|
||||
@app.websocket("/ws")
|
||||
async def websocket_endpoint(websocket: WebSocket):
|
||||
async def websocketEndpoint(websocket: WebSocket):
|
||||
await websocket.accept()
|
||||
connected_clients.add(websocket)
|
||||
connectedClients.add(websocket)
|
||||
try:
|
||||
while True:
|
||||
await websocket.receive_text()
|
||||
except WebSocketDisconnect:
|
||||
connected_clients.discard(websocket)
|
||||
connectedClients.discard(websocket)
|
||||
|
||||
Reference in New Issue
Block a user