|
@@ -1,3 +1,4 @@
|
|
|
+import asyncio
|
|
|
import gzip
|
|
|
import logging
|
|
|
import re
|
|
@@ -107,23 +108,26 @@ class Air:
|
|
|
await self.connect()
|
|
|
|
|
|
async def connect(self) -> None:
|
|
|
- try:
|
|
|
- if self.relay.connected:
|
|
|
+ backoff_time = 1
|
|
|
+ while True:
|
|
|
+ try:
|
|
|
+ if self.relay.connected:
|
|
|
+ await self.relay.disconnect()
|
|
|
+ await self.relay.connect(
|
|
|
+ f'{RELAY_HOST}?device_token={self.token}',
|
|
|
+ socketio_path='/on_air/socket.io',
|
|
|
+ transports=['websocket', 'polling'], # favor websocket over polling
|
|
|
+ )
|
|
|
+ break
|
|
|
+ except socketio.exceptions.ConnectionError:
|
|
|
+ pass
|
|
|
+ except ValueError: # NOTE this sometimes happens when the internal socketio client is not yet ready
|
|
|
await self.relay.disconnect()
|
|
|
- await self.relay.connect(
|
|
|
- f'{RELAY_HOST}?device_token={self.token}',
|
|
|
- socketio_path='/on_air/socket.io',
|
|
|
- transports=['websocket', 'polling'],
|
|
|
- )
|
|
|
- except socketio.exceptions.ConnectionError:
|
|
|
- await self.connect()
|
|
|
- except ValueError: # NOTE this sometimes happens when the internal socketio client is not yet ready
|
|
|
- await self.relay.disconnect()
|
|
|
- await self.connect()
|
|
|
- except Exception:
|
|
|
- logging.exception('Could not connect to NiceGUI On Air server.')
|
|
|
- print('Could not connect to NiceGUI On Air server.', flush=True)
|
|
|
- await self.connect()
|
|
|
+ except Exception:
|
|
|
+ globals.log.exception('Could not connect to NiceGUI On Air server.')
|
|
|
+
|
|
|
+ await asyncio.sleep(backoff_time)
|
|
|
+ backoff_time = min(backoff_time * 2, 32)
|
|
|
|
|
|
async def disconnect(self) -> None:
|
|
|
await self.relay.disconnect()
|