Пример #1
0
    async def create_broker_connection(self, url):
        """Create an asynchronous connection to the broker."""
        while True:
            try:
                self.broker = await websocket_connect(
                    url,
                    subprotocols=['token',
                                  auth_token(self.keys).decode()])
            except ConnectionRefusedError:
                logger.warning("Cannot connect, retrying in 3s")
            else:
                logger.info("Connected to broker, waiting for incoming "
                            "messages")
                # Start the periodic send of websocket alive messages
                callback = PeriodicCallback(self.send_alive,
                                            ALIVE_PERIOD * 1000)
                callback.start()
                self.fetch_nodes_cache('all')
                while True:
                    message = await self.broker.read_message()
                    if message is None:
                        logger.warning("Connection with broker lost.")
                        callback.stop()
                        break
                    await self.on_broker_message(message)

            await gen.sleep(3)
Пример #2
0
    def create_broker_connection(self, url):
        """Create an asynchronous connection to the broker."""
        while True:
            try:
                self.broker = yield websocket_connect(url)
            except ConnectionRefusedError:
                logger.debug("Cannot connect, retrying in 3s")
            else:
                logger.debug("Connected to broker, sending auth token")
                self.broker.write_message(auth_token(self.keys))
                while True:
                    message = yield self.broker.read_message()
                    if message is None:
                        logger.debug("Connection with broker lost.")
                        break
                    self.on_broker_message(message)

            yield gen.sleep(3)