Skip to content

Commit b4f3614

Browse files
Optional connection retries (Fixes #1306)
1 parent 5f00188 commit b4f3614

File tree

2 files changed

+18
-4
lines changed

2 files changed

+18
-4
lines changed

src/socketio/async_client.py

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -69,7 +69,7 @@ def is_asyncio_based(self):
6969

7070
async def connect(self, url, headers={}, auth=None, transports=None,
7171
namespaces=None, socketio_path='socket.io', wait=True,
72-
wait_timeout=1):
72+
wait_timeout=1, retry=False):
7373
"""Connect to a Socket.IO server.
7474
7575
:param url: The URL of the Socket.IO server. It can include custom
@@ -105,6 +105,8 @@ async def connect(self, url, headers={}, auth=None, transports=None,
105105
connection. The default is 1 second. This
106106
argument is only considered when ``wait`` is set
107107
to ``True``.
108+
:param retry: Apply the reconnection logic if the initial connection
109+
attempt fails. The default is ``False``.
108110
109111
Note: this method is a coroutine.
110112
@@ -147,6 +149,10 @@ async def connect(self, url, headers={}, auth=None, transports=None,
147149
await self._trigger_event(
148150
'connect_error', n,
149151
exc.args[1] if len(exc.args) > 1 else exc.args[0])
152+
if retry: # pragma: no cover
153+
await self._handle_reconnect()
154+
if self.eio.state == 'connected':
155+
return
150156
raise exceptions.ConnectionError(exc.args[0]) from None
151157

152158
if wait:
@@ -477,7 +483,8 @@ async def _handle_reconnect(self):
477483
auth=self.connection_auth,
478484
transports=self.connection_transports,
479485
namespaces=self.connection_namespaces,
480-
socketio_path=self.socketio_path)
486+
socketio_path=self.socketio_path,
487+
retry=False)
481488
except (exceptions.ConnectionError, ValueError):
482489
pass
483490
else:

src/socketio/client.py

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -69,7 +69,7 @@ class Client(base_client.BaseClient):
6969
"""
7070
def connect(self, url, headers={}, auth=None, transports=None,
7171
namespaces=None, socketio_path='socket.io', wait=True,
72-
wait_timeout=1):
72+
wait_timeout=1, retry=False):
7373
"""Connect to a Socket.IO server.
7474
7575
:param url: The URL of the Socket.IO server. It can include custom
@@ -105,6 +105,8 @@ def connect(self, url, headers={}, auth=None, transports=None,
105105
connection. The default is 1 second. This
106106
argument is only considered when ``wait`` is set
107107
to ``True``.
108+
:param retry: Apply the reconnection logic if the initial connection
109+
attempt fails. The default is ``False``.
108110
109111
Example usage::
110112
@@ -145,6 +147,10 @@ def connect(self, url, headers={}, auth=None, transports=None,
145147
self._trigger_event(
146148
'connect_error', n,
147149
exc.args[1] if len(exc.args) > 1 else exc.args[0])
150+
if retry: # pragma: no cover
151+
self._handle_reconnect()
152+
if self.eio.state == 'connected':
153+
return
148154
raise exceptions.ConnectionError(exc.args[0]) from None
149155

150156
if wait:
@@ -441,7 +447,8 @@ def _handle_reconnect(self):
441447
auth=self.connection_auth,
442448
transports=self.connection_transports,
443449
namespaces=self.connection_namespaces,
444-
socketio_path=self.socketio_path)
450+
socketio_path=self.socketio_path,
451+
retry=False)
445452
except (exceptions.ConnectionError, ValueError):
446453
pass
447454
else:

0 commit comments

Comments
 (0)