Пример #1
0
    def main(self):

        # create event loop
        app = Qt.QApplication(sys.argv)
        loop = QEventLoop(app)
        asyncio.set_event_loop(loop)  # NEW must set the event loop

        # authentication process
        auth_ = ClientAuth(db_path=self.db_path)
        login_wnd = LoginWindow(auth_instance=auth_)

        if login_wnd.exec_() == QtWidgets.QDialog.Accepted:
            # Each client will create a new protocol instance
            client_ = ChatClientProtocol(db_path=self.db_path,
                                         loop=loop,
                                         username=login_wnd.username,
                                         password=login_wnd.password)

            # create Contacts window
            wnd = ContactsWindow(client_instance=client_, user_name=login_wnd.username)
            client_.gui_instance = wnd  # reference from protocol to GUI, for msg update

            with loop:
                # cleaning old instances
                del auth_
                del login_wnd

                # connect to our server
                try:
                    coro = loop.create_connection(lambda: client_, self.args["addr"], self.args["port"])
                    server = loop.run_until_complete(coro)
                except ConnectionRefusedError:
                    print('Error. wrong server')
                    exit(1)

                # start GUI client
                wnd.show()
                client_.get_from_gui()  # asyncio.ensure_future(client_.get_from_gui(loop))

                # Serve requests until Ctrl+C
                try:
                    loop.run_forever()
                except KeyboardInterrupt:
                    pass
                except Exception:
                    pass
Пример #2
0
class App(QApplication):
    def __init__(self):
        QApplication.__init__(self, sys.argv)
        self.loop = QEventLoop(self)
        asyncio.set_event_loop(self.loop)

        self.client = Client(self.loop, f'user-{int(random()*10000)}')
        self.loop.create_task(self.start())

        self.gui = MyWindow(self.loop, self.client)
        self.gui.show()
        self.loop.run_forever()

    async def start(self):
        clientConnection = self.loop.create_connection(lambda: self.client,
                                                       '127.0.0.1', 8888)
        await asyncio.wait_for(clientConnection, 10000, loop=self.loop)
Пример #3
0
class App(QApplication):
    def __init__(self):
        QApplication.__init__(self, sys.argv)
        self.loop = QEventLoop(self)
        asyncio.set_event_loop(self.loop)
        self.userClient = EchoClientProtocol(self.loop, 'user')

        self.loop.create_task(self.start())
        self.gui = Gui(self.loop, self.userClient)

        self.loop.run_forever()

    async def start(self):
        clientConnection = self.loop.create_connection(lambda: self.userClient,
                                                       '127.0.0.1', 8888)
        await asyncio.wait_for(clientConnection, 1000)
        transport, protocol = clientConnection
        self.userClient.connection_made(transport)
Пример #4
0
class App(QApplication):
    def __init__(self):
        QApplication.__init__(self, sys.argv)
        self.loop = QEventLoop(self)
        asyncio.set_event_loop(self.loop)
        self.userClient = EchoClientProtocol(self.loop, 'user')

        self.loop.create_task(self.start())

        self.window = MyWindow(self.loop, self.userClient)
        self.window.show()
        self.loop.run_forever()

    async def start(self):
        print('[START CLIENT]')
        clientConnection = self.loop.create_connection(lambda: self.userClient,
                                                       '127.0.0.1', 8888)

        await asyncio.wait_for(clientConnection, 10000, loop=self.loop)