durch verbundene Websockets, findet einen mit passender Route und fügt die Rückgabedaten zu einem Wörterbuch, das den übereinstimmenden Socket enthält, hinzu. Andere. < /li>
Frontend -Updates basierend auf Daten, die von Socket empfangen werden. Hat jemand mit Erfahrung eine gute Lösung/Empfehlung, um diese Funktion zu machen?
Code: Select all
def __init__(self):
self.activate_connections: list[WebSocket] = []
async def connect(self, websocket: WebSocket, route: str):
await websocket.accept()
self.activate_connections.append({
'socket': websocket,
'route': route
})
async def disconnect(self, websocket: WebSocket, route: str):
for socket in self.activate_connections:
if (socket['route'] == route):
self.activate_connections.remove(socket)
async def addReturnedData(self, data: dict, route: str):
for connection in self.activate_connections:
print(connection)
if connection['route'] == route:
connection['data'] = data
async def broadcast(self, newData: dict):
print(newData)
jsonData = json.dumps(newData)
print(jsonData)
for connection in self.activate_connections:
await connection['socket'].send_json(jsonData)