I try to ping/pong json packages between a FastAPI websocket endpoint and a client using the websocket library from Zephyr RTOS. Unfortunately the connection is closed by the FastAPI server after ~40 seconds. This time seems to be constant. My guess is, that there must occur a timeout event due to a missing/wrong condition.
This post seems to address my issue. Unfortunately my code is not running with a newer uvicorn version which would support --ws-ping-interval or --ws-ping-timeout.
For a simple test I did the following:
Server side:
@app.websocket("/ws/update_status")
async def websocket_endpoint(websocket: WebSocket, db: Session = Depends(get_db)):
await websocket.accept()
while True:
data = await websocket.receive_json()
await websocket.send_json('{"status":"ok"}')
Client side (C code running on Zephyr-RTOS):
// some code to define JSON package
while(1)
{
websocket_send_msg(sock, send_buffer, json_length, WEBSOCKET_OPCODE_DATA_TEXT, true, true, SYS_FOREVER_MS);
websocket_recv_msg(sock, receive_buffer, sizeof(receive_buffer), &message_type, 0, SYS_FOREVER_MS);
}
I use the following package versions:
uvicorn=='0.13.4'
fastapi=='0.68.2'
Does anyone know why the connection aborts after 40 seconds and how it can be avoided?