async def keep_connected(self): """ A continuous loop that keeps the connection open """ while True: logger.debug(f"in keep_connected, proto={self.proto}") if self.proto and self.proto.open: pass else: # xxx should we close() our client ? self.proto = None # see if we need ssl secure = websockets.uri.parse_uri(self.url).secure kwds = {} if secure: import ssl kwds.update(dict(ssl=ssl.SSLContext())) try: logger.info(f"(re)-connecting to {self.url} ...") self.proto = await SidecarAsyncClient(self.url, **kwds) logger.debug("connected !") except ConnectionRefusedError: logger.warning( f"Could not connect to {self.url} at this time") except: logger.exception( f"Could not connect to {self.url} at this time") await asyncio.sleep(self.keep_period)
async def emit_infos(self, infos): if not self.proto: logger.warning(f"dropping message {infos}") return False logger.debug(f"Sending {infos}") # xxx use Payload payload = dict(category=self.category, action='info', message=infos) # xxx try/except here try: await self.proto.send(json.dumps(payload)) self.counter += 1 except ConnectionRefusedError: logger.warning(f"Could not send {self.category} - dropped") except Exception as exc: # xxx to review logger.exception("send failed") self.proto = None return False return True
def on_channel(self, channel, *args): # not supposed to run - should be redefined logger.warning("ReconnectableSocketIO.on_channel, channel={}, args={}".format(channel, args))
def on_channel(self, channel, *args): if channel == back_channel: self.monitor_leases.on_back_channel(*args) else: logger.warning("received data {} on unexpected channel {}".format(channel))