Hello gouys, how are you?
I developed an integration based on the exempla EventsAndStateWebSocketApiPython. But Constantly I lost the connection with the server and I don’t connect again, it’s needed to reset the Events Server in the Milestone server application to connect again and continuing receiving the events.
Below are the part of my code where I connect with webscokets and mantaining the applicationg receiving the events. To you know the erros are always 401, 502 and 503.
async def main(ip_address, access_token, verify_ssl, controller, milestone_thread: IoMilestoneCommunication):
session_id = ""
last_event_id = ""
while not milestone_thread.do_stop:
try:
escape_task = asyncio.create_task(check_for_escape(ip_address, access_token))
connection_uri = f"wss://{ip_address}/api/ws/events/v1/" if verify_ssl else f"ws://{ip_address}/api/ws/events/v1/"
receive_events_task = None
async with connect(connection_uri, extra_headers={"Authorization": f"Bearer {access_token}"}) as web_socket:
session = await start_session(web_socket, session_id, last_event_id)
state_events = []
if session["status"] == 201:
subscription = await create_subscription(web_socket, subscription_filters)
milestone_thread.trace(f"Created Subscription => {subscription}")
session_id = session["sessionId"]
await process_events(state_events, ip_address, access_token, controller)
while not escape_task.done():
receive_events_task = asyncio.create_task(receive_events(web_socket))
done, pending = await asyncio.wait([receive_events_task, escape_task], return_when=asyncio.FIRST_COMPLETED)
if escape_task.done():
await web_socket.close()
trace("Reconnection triggered by escape task.")
break
if receive_events_task in done:
events = await receive_events_task
last_event_id = events["events"][-1]["id"]
await process_events(events["events"], ip_address, access_token, controller)
if receive_events_task is not None:
await receive_events_task
except asyncio.CancelledError:
trace("Task was cancelled. Retrying...")
await asyncio.sleep(1)
continue
except (ConnectionClosedOK, ConnectionClosedError):
trace("[*] WebSocket connection closed. Reconnecting...")
await asyncio.sleep(1)
except websockets.exceptions.InvalidStatusCode as e:
trace(f"InvalidStatusCode exception: {e}. HTTP {e.status_code}")
if e.status_code == 401:
trace("[*] Re-authenticating due to HTTP 401 error.")
milestone_thread.token = milestone_thread.login()
await asyncio.sleep(1)
elif e.status_code in (502, 503):
trace(f"[*] HTTP {e.status_code} error. Backing off before retrying...")
await asyncio.sleep(10)
else:
await asyncio.sleep(5)
except asyncio.exceptions.TimeoutError:
trace("[!] Timeout error. Retrying connection...")
await asyncio.sleep(2)
except Exception as e:
report_exception(e)
trace(f"[!] General Exception: {e}")
await asyncio.sleep(2)
finally:
if not escape_task.done():
escape_task.cancel()
try:
await escape_task
except asyncio.CancelledError:
trace("[*] Escape task cancelled.")
Software Version: 2023 R3 (23.3a)
Can you guys already seen this problem?
Best Regards,
João Pedro