Fix disconnect and reconnect for exported senders (#53)

This commit is contained in:
Lonami Exo 2017-05-30 13:27:23 +02:00
parent 097d1669b2
commit 6b4f2abb96
2 changed files with 11 additions and 6 deletions

View File

@ -51,7 +51,11 @@ class InteractiveTelegramClient(TelegramClient):
self.found_media = set()
print('Connecting to Telegram servers...')
self.connect()
if not self.connect():
print('Initial connection failed. Retrying...')
if not self.connect():
print('Could not connect to Telegram servers.')
return
# Then, ensure we're authorized and have access
if not self.is_user_authorized():

View File

@ -147,7 +147,8 @@ class TelegramClient:
self._setup_ping_thread()
return True
except RPCError as error:
except (RPCError, ConnectionError) as error:
# Probably errors from the previous session, ignore them
self._logger.warning('Could not stabilise initial connection: {}'
.format(error))
return False
@ -163,7 +164,7 @@ class TelegramClient:
self.transport = None
# Also disconnect all the cached senders
for sender in self._cached_senders:
for sender in self._cached_senders.values():
sender.disconnect()
self._cached_senders.clear()
@ -196,7 +197,7 @@ class TelegramClient:
if not self.dc_options:
raise ConnectionError(
'Cannot determine the required data center IP address. '
'Stabilise an initial connection first.')
'Stabilise a successful initial connection first.')
return next(dc for dc in self.dc_options if dc.id == dc_id)
@ -218,8 +219,8 @@ class TelegramClient:
if sender and session:
if init_connection:
# TODO reconnect
pass
sender.disconnect()
sender.connect()
return sender
else: