コード例 #1
0
    def run(self, loop=None, no_polling=False, no_forever=False):
        self.checkEventBodyAnnotations()
        self.checkEventDependencies()

        loop = loop or asyncio.get_event_loop()
        self.queue = queue = asyncio.Queue(loop=loop)
        exit_signal = False
        loop.run_until_complete(self.enable_session())
        if not no_polling:
            if not self.useWebsocket:
                SessionLogger.warning(
                    "http's fetchMessage is disabled in mirai-api-http 1.2.1(it's a bug :P)."
                )
                SessionLogger.warning("so, you can use WebSocket.")
                SessionLogger.warning(
                    "if it throw a unexpected error, you should call the httpapi's author."
                )
                loop.create_task(
                    self.message_polling(lambda: exit_signal, queue))
            else:
                SessionLogger.warning(
                    "you are using WebSocket, it's a experimental method.")
                SessionLogger.warning(
                    "but, websocket is remember way to fetch message and event,"
                )
                SessionLogger.warning(
                    "and http's fetchMessage is disabled in mirai-api-http 1.2.1(it's a bug :P)."
                )
                SessionLogger.warning(
                    "if it throw a unexpected error, you can call the httpapi's author."
                )
                loop.create_task(
                    self.ws_event_receiver(lambda: exit_signal, queue))
            loop.create_task(self.event_runner(lambda: exit_signal, queue))

        if not no_forever:
            for i in self.subroutines:
                loop.create_task(i(self))

        try:
            loop.run_forever()
        except KeyboardInterrupt:
            SessionLogger.info("catched Ctrl-C, exiting..")
        finally:
            loop.run_until_complete(self.release())
            loop.run_until_complete(session.close())
コード例 #2
0
ファイル: application.py プロジェクト: wyapx/python-mirai
    def run(self, loop=None):
        self.checkEventBodyAnnotations()
        self.checkEventDependencies()

        loop = loop or asyncio.get_event_loop()
        queue = asyncio.Queue(loop=loop)
        exit_signal = False
        loop.run_until_complete(self.enable_session())
        loop.create_task(self.message_polling(lambda: exit_signal, queue))
        loop.create_task(self.event_runner(lambda: exit_signal, queue))
        for i in self.run_forever_target:
            loop.create_task(i(self))

        try:
            loop.run_forever()
        except KeyboardInterrupt:
            SessionLogger.info("catched Ctrl-C, exiting..")
        finally:
            loop.run_until_complete(self.release())
            loop.run_until_complete(session.close())
コード例 #3
0
    def run(self, loop=None, no_polling=False, no_forever=False):
        self.checkEventBodyAnnotations()
        self.checkEventDependencies()

        loop = loop or asyncio.get_event_loop()
        self.queue = asyncio.Queue(loop=loop)
        exit_signal = False
        loop.run_until_complete(self.enable_session())
        if not no_polling:
            # check ws status
            if self.useWebsocket:
                SessionLogger.info("event receive method: websocket")
            else:
                SessionLogger.info("event receive method: http polling")

            result = loop.run_until_complete(self.checkWebsocket())
            if not result:  # we can use http, not ws.
                # should use http, but we can change it.
                if self.useWebsocket:
                    SessionLogger.warning("catched wrong config: enableWebsocket=false, we will modify it.")
                    loop.run_until_complete(self.setConfig(enableWebsocket=True))
                    loop.create_task(self.ws_event())
                    loop.create_task(self.ws_message())
                else:
                    loop.create_task(self.message_polling())
            else:  # we can use websocket, it's fine
                if self.useWebsocket:
                    loop.create_task(self.ws_event())
                    loop.create_task(self.ws_message())
                else:
                    SessionLogger.warning("catched wrong config: enableWebsocket=true, we will modify it.")
                    loop.run_until_complete(self.setConfig(enableWebsocket=False))
                    loop.create_task(self.message_polling())
            loop.create_task(self.event_runner())

        if not no_forever:
            for i in self.subroutines:
                loop.create_task(i(self))

        try:
            for start_callable in self.lifecycle['start']:
                loop.run_until_complete(self.run_func(start_callable, self))

            for around_callable in self.lifecycle['around']:
                loop.run_until_complete(self.run_func(around_callable, self))

            loop.run_forever()
        except KeyboardInterrupt:
            SessionLogger.info("catched Ctrl-C, exiting..")
        except Exception as e:
            traceback.print_exc()
        finally:
            for around_callable in self.lifecycle['around']:
                loop.run_until_complete(self.run_func(around_callable, self))

            for end_callable in self.lifecycle['end']:
                loop.run_until_complete(self.run_func(end_callable, self))

            loop.run_until_complete(self.release())