Notice when connection runs out of retries
This commit is contained in:
parent
df72a3d9cf
commit
d55d05826f
3 changed files with 33 additions and 13 deletions
|
|
@ -77,6 +77,11 @@ class Bot(Inhabitant):
|
||||||
if room:
|
if room:
|
||||||
await room.exit()
|
await room.exit()
|
||||||
|
|
||||||
|
# INHABITED FUNCTIONS
|
||||||
|
|
||||||
|
async def on_stopped(self, room):
|
||||||
|
await self.part_room(room.roomname)
|
||||||
|
|
||||||
# BOTRULEZ
|
# BOTRULEZ
|
||||||
|
|
||||||
@command("ping", specific=False, args=False)
|
@command("ping", specific=False, args=False)
|
||||||
|
|
|
||||||
|
|
@ -12,10 +12,11 @@ __all__ = ["Connection"]
|
||||||
|
|
||||||
|
|
||||||
class Connection:
|
class Connection:
|
||||||
def __init__(self, url, packet_callback, disconnect_callback, cookiejar=None, ping_timeout=10, ping_delay=30, reconnect_attempts=10):
|
def __init__(self, url, packet_callback, disconnect_callback, stop_callback, cookiejar=None, ping_timeout=10, ping_delay=30, reconnect_attempts=10):
|
||||||
self.url = url
|
self.url = url
|
||||||
self.packet_callback = packet_callback
|
self.packet_callback = packet_callback
|
||||||
self.disconnect_callback = disconnect_callback
|
self.disconnect_callback = disconnect_callback
|
||||||
|
self.stop_callback = stop_callback # is called when the connection stops on its own
|
||||||
self.cookiejar = cookiejar
|
self.cookiejar = cookiejar
|
||||||
self.ping_timeout = ping_timeout # how long to wait for websocket ping reply
|
self.ping_timeout = ping_timeout # how long to wait for websocket ping reply
|
||||||
self.ping_delay = ping_delay # how long to wait between pings
|
self.ping_delay = ping_delay # how long to wait between pings
|
||||||
|
|
@ -65,9 +66,10 @@ class Connection:
|
||||||
This means that stop() can only be called once.
|
This means that stop() can only be called once.
|
||||||
"""
|
"""
|
||||||
|
|
||||||
self._stopped = True
|
if not self._stopped:
|
||||||
await self.reconnect() # _run() does the cleaning up now.
|
self._stopped = True
|
||||||
await self._runtask
|
await self.reconnect() # _run() does the cleaning up now.
|
||||||
|
await self._runtask
|
||||||
|
|
||||||
async def reconnect(self):
|
async def reconnect(self):
|
||||||
"""
|
"""
|
||||||
|
|
@ -153,16 +155,22 @@ class Connection:
|
||||||
"""
|
"""
|
||||||
|
|
||||||
while not self._stopped:
|
while not self._stopped:
|
||||||
await self._connect(self.reconnect_attempts)
|
connected = await self._connect(self.reconnect_attempts)
|
||||||
logger.debug(f"{self.url}:Connected")
|
if connected:
|
||||||
|
logger.debug(f"{self.url}:Connected")
|
||||||
|
try:
|
||||||
|
while True:
|
||||||
|
await self._handle_next_message()
|
||||||
|
except websockets.ConnectionClosed:
|
||||||
|
pass
|
||||||
|
finally:
|
||||||
|
await self._disconnect() # disconnect and clean up
|
||||||
|
else:
|
||||||
|
logger.debug(f"{self.url}:Stopping")
|
||||||
|
asyncio.ensure_future(self.stop_callback)
|
||||||
|
self._stopped = True
|
||||||
|
await self._disconnect()
|
||||||
|
|
||||||
try:
|
|
||||||
while True:
|
|
||||||
await self._handle_next_message()
|
|
||||||
except websockets.ConnectionClosed:
|
|
||||||
pass
|
|
||||||
finally:
|
|
||||||
await self._disconnect() # disconnect and clean up
|
|
||||||
|
|
||||||
async def _ping(self):
|
async def _ping(self):
|
||||||
"""
|
"""
|
||||||
|
|
|
||||||
|
|
@ -58,6 +58,7 @@ class Room:
|
||||||
self.format_room_url(self.roomname, human=self.human),
|
self.format_room_url(self.roomname, human=self.human),
|
||||||
self._receive_packet,
|
self._receive_packet,
|
||||||
self._disconnected,
|
self._disconnected,
|
||||||
|
self._stopped,
|
||||||
cookiejar
|
cookiejar
|
||||||
)
|
)
|
||||||
|
|
||||||
|
|
@ -172,6 +173,9 @@ class Room:
|
||||||
|
|
||||||
await self._inhabitant.on_disconnected(self)
|
await self._inhabitant.on_disconnected(self)
|
||||||
|
|
||||||
|
async def _stopped(self):
|
||||||
|
await self._inhabitant.on_stopped(self)
|
||||||
|
|
||||||
async def _receive_packet(self, ptype, data, error, throttled):
|
async def _receive_packet(self, ptype, data, error, throttled):
|
||||||
# Ignoring errors and throttling for now
|
# Ignoring errors and throttling for now
|
||||||
functions = {
|
functions = {
|
||||||
|
|
@ -410,6 +414,9 @@ class Inhabitant:
|
||||||
async def on_disconnected(self, room):
|
async def on_disconnected(self, room):
|
||||||
pass
|
pass
|
||||||
|
|
||||||
|
async def on_stopped(self, room):
|
||||||
|
pass
|
||||||
|
|
||||||
async def on_join(self, room, session):
|
async def on_join(self, room, session):
|
||||||
pass
|
pass
|
||||||
|
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue