@@ -34,6 +34,7 @@ def __init__(self, ip, port=49476):
3434 self ._backoff = 0 # exponential backoff
3535 self ._transport = None # UDP transport/socket
3636 self ._reconnect = None # reconnect timer
37+ self ._inactive = None # inactivity timer
3738 self ._disconnecting = False # disconnecting flag
3839 self ._was_connected = False # 'disconnected' event armed?
3940
@@ -58,6 +59,10 @@ async def _close_connection(self, unsub = True):
5859 self ._reconnect .cancel ()
5960 self ._reconnect = None
6061
62+ if self ._inactive is not None :
63+ self ._inactive .cancel ()
64+ self ._inactive = None
65+
6166 if self ._transport is not None :
6267 if unsub :
6368 self ._transport .sendto (b'subscribe(0)\n ' )
@@ -93,6 +98,9 @@ def _send_subscribe(self):
9398 if self ._transport is not None :
9499 self ._transport .sendto (b'subscribe(60)\n ' )
95100
101+ def _on_inactivity (self ):
102+ asyncio .create_task (self ._close_connection ())
103+
96104 # DatagramProtocol support below
97105
98106 def protocol_factory (self ):
@@ -112,6 +120,11 @@ def datagram_received(self, data, addr):
112120 if not self ._was_connected :
113121 self ._was_connected = True
114122
123+ if self ._inactive is not None :
124+ self ._inactive .cancel ()
125+ loop = asyncio .get_running_loop ()
126+ self ._inactive = loop .call_later (60 , self ._on_inactivity )
127+
115128 lines = data .decode ('utf-8' ).splitlines ()
116129 for line in lines :
117130 try :
0 commit comments