Add periodic pings if an updates thread was started (closes #138)

This commit is contained in:
Lonami Exo 2017-06-20 09:46:20 +02:00
parent 697434be37
commit 86358d7805

View File

@ -1,7 +1,7 @@
from datetime import timedelta
from mimetypes import guess_type
from threading import Event, RLock, Thread
from time import sleep
from time import sleep, time
from . import TelegramBareClient
@ -35,6 +35,9 @@ from .tl.functions.messages import (
# For .get_me() and ensuring we're authorized
from .tl.functions.users import GetUsersRequest
# So the server doesn't stop sending updates to us
from .tl.functions import PingRequest
# All the types we need to work with
from .tl.types import (
ChatPhotoEmpty, DocumentAttributeAudio, DocumentAttributeFilename,
@ -94,11 +97,14 @@ class TelegramClient(TelegramBareClient):
# Safety across multiple threads (for the updates thread)
self._lock = RLock()
# Methods to be called when an update is received
# Updates-related members
self._update_handlers = []
self._updates_thread_running = Event()
self._updates_thread_receiving = Event()
self._next_ping_at = 0
self.ping_interval = 60 # Seconds
# Used on connection - the user may modify these and reconnect
if device_model:
self.session.device_model = device_model
@ -805,6 +811,10 @@ class TelegramClient(TelegramBareClient):
'Trying to receive updates from the updates thread'
)
if time() > self._next_ping_at:
self._next_ping_at = time() + self.ping_interval
self.invoke(PingRequest(utils.generate_random_long()))
updates = self.sender.receive_updates(timeout=timeout)
self._updates_thread_receiving.clear()