I have 3 clients, who periodically send data to my server. I use the example from FastAPI's documentation.
This is my server code:
class ConnectionManager:
def __init__(self):
self.active_connections: list[WebSocket] = []
async def connect(self, websocket: WebSocket):
await websocket.accept()
self.active_connections.append(websocket)
def disconnect(self, websocket: WebSocket):
self.active_connections.remove(websocket)
async def send_response(self, message: dict, websocket: WebSocket):
await websocket.send_json(message)
async def broadcast(self, message: str):
for connection in self.active_connections:
await connection.send_text(message)
manager = ConnectionManager()
@app.websocket("/chargeStationState/{client_id}")
async def websocket_endpoint(websocket: WebSocket,
client_id: int,
db: Session = Depends(deps.get_db)):
await manager.connect(websocket)
try:
while True:
message = await websocket.receive_json()
logging.info(message)
## read data from db
response = {
"stations": "repsonse",
"timestamp": int(time.time())
}
await manager.send_response(response, websocket)
#await manager.broadcast(f"Client #{client_id} says: {data}")
except WebSocketDisconnect:
manager.disconnect(websocket)
This is the client code:
async for websocket in websockets.connect("ws://127.0.0.1:8001/chargeStationState/1"):
message = {'name':'station1'}
await websocket.send(json.dumps(message))
p = await asyncio.wait_for(websocket.recv(), timeout=10)
print(p)
await asyncio.sleep(2)
So, I would like to have 5 clients, who will talk to my server and send sensor data, but after 5 minutes I get the following error
websockets.exceptions.ConnectionClosedOK: received 1000 (OK)
then receiving 1000 (OK)
and cannot identify where the issue lies.