mirror of
				https://github.com/LonamiWebs/Telethon.git
				synced 2025-10-30 23:47:33 +03:00 
			
		
		
		
	Add periodic pings if an updates thread was started (closes #138)
This commit is contained in:
		
							parent
							
								
									697434be37
								
							
						
					
					
						commit
						86358d7805
					
				|  | @ -1,7 +1,7 @@ | ||||||
| from datetime import timedelta | from datetime import timedelta | ||||||
| from mimetypes import guess_type | from mimetypes import guess_type | ||||||
| from threading import Event, RLock, Thread | from threading import Event, RLock, Thread | ||||||
| from time import sleep | from time import sleep, time | ||||||
| 
 | 
 | ||||||
| from . import TelegramBareClient | from . import TelegramBareClient | ||||||
| 
 | 
 | ||||||
|  | @ -35,6 +35,9 @@ from .tl.functions.messages import ( | ||||||
| # For .get_me() and ensuring we're authorized | # For .get_me() and ensuring we're authorized | ||||||
| from .tl.functions.users import GetUsersRequest | 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 | # All the types we need to work with | ||||||
| from .tl.types import ( | from .tl.types import ( | ||||||
|     ChatPhotoEmpty, DocumentAttributeAudio, DocumentAttributeFilename, |     ChatPhotoEmpty, DocumentAttributeAudio, DocumentAttributeFilename, | ||||||
|  | @ -94,11 +97,14 @@ class TelegramClient(TelegramBareClient): | ||||||
|         # Safety across multiple threads (for the updates thread) |         # Safety across multiple threads (for the updates thread) | ||||||
|         self._lock = RLock() |         self._lock = RLock() | ||||||
| 
 | 
 | ||||||
|         # Methods to be called when an update is received |         # Updates-related members | ||||||
|         self._update_handlers = [] |         self._update_handlers = [] | ||||||
|         self._updates_thread_running = Event() |         self._updates_thread_running = Event() | ||||||
|         self._updates_thread_receiving = 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 |         # Used on connection - the user may modify these and reconnect | ||||||
|         if device_model: |         if device_model: | ||||||
|             self.session.device_model = device_model |             self.session.device_model = device_model | ||||||
|  | @ -805,6 +811,10 @@ class TelegramClient(TelegramBareClient): | ||||||
|                         'Trying to receive updates from the updates thread' |                         '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) |                     updates = self.sender.receive_updates(timeout=timeout) | ||||||
| 
 | 
 | ||||||
|                     self._updates_thread_receiving.clear() |                     self._updates_thread_receiving.clear() | ||||||
|  |  | ||||||
		Loading…
	
		Reference in New Issue
	
	Block a user