From 58fe39f06036052e651e916155da1ba8dcf95529 Mon Sep 17 00:00:00 2001 From: Lonami Exo Date: Sun, 17 Sep 2017 16:39:29 +0200 Subject: [PATCH] Expose MtProtoSender.is_connected() and use it on .connect() --- telethon/telegram_bare_client.py | 14 +++++--------- 1 file changed, 5 insertions(+), 9 deletions(-) diff --git a/telethon/telegram_bare_client.py b/telethon/telegram_bare_client.py index 9d0c7bc5..0990fa27 100644 --- a/telethon/telegram_bare_client.py +++ b/telethon/telegram_bare_client.py @@ -103,15 +103,8 @@ class TelegramBareClient: If 'exported_auth' is not None, it will be used instead to determine the authorization key for the current session. """ - if self._sender and self._sender.is_connected(): - # Try sending a ping to make sure we're connected already - # TODO Maybe there's a better way to check this - try: - self(PingRequest(utils.generate_random_long())) - return True - except: - # If ping failed, ensure we're disconnected first - self.disconnect() + if self.is_connected(): + return True connection = Connection( self.session.server_address, self.session.port, @@ -168,6 +161,9 @@ class TelegramBareClient: ) return False + def is_connected(self): + return self._sender is not None and self._sender.is_connected() + def _init_connection(self, query=None): result = self(InvokeWithLayerRequest(LAYER, InitConnectionRequest( api_id=self.api_id,