示例#1
0
async def handle_connection(queues, account_hash):
    while True:
        queues['watchdog'].put_nowait('Connection is alive. Source: Prompt before auth')

        async with open_asyncio_connection(HOST, WRITE_PORT) as rw_descriptor:

            reader, writer = rw_descriptor

            await reader.readline()

            try:
                account_dict = await authorise(reader, writer, account_hash)
            except InvalidToken:
                messagebox.showinfo("Ошибка авторизации", "Проверьте токен, сервер его не узнал.")

        queues['watchdog'].put_nowait('Connection is alive. Source: Authorization done')

        queues['messages'].put_nowait('Выполнена авторизация. Пользователь {}.\n'.format(account_dict['nickname']))

        loaded_messages = read_file(HISTORY_FILE)
        queues['messages'].put_nowait(loaded_messages)

        nickname = gui.NicknameReceived(account_dict['nickname'])
        queues['status_updates'].put_nowait(nickname)

        async with anyio.create_task_group() as tg:

            await tg.spawn(read_msgs, HOST, READ_PORT, queues)
            await tg.spawn(save_messages, HISTORY_FILE, queues['saving'])
            await tg.spawn(send_msgs, HOST, WRITE_PORT, queues, account_hash)
            await tg.spawn(watch_for_connection, queues['watchdog'])
            await tg.spawn(ping_pong, reader, writer)
示例#2
0
async def read_chat(host, port, filename):
    async with open_asyncio_connection(host, port, filename) as rw_descriptor:
        reader, writer = rw_descriptor

        while True:
            data = await reader.readline()

            message = data.decode()
            print(message.strip())

            await write_file(filename, handle_text(message))
async def write_to_chat(host, port, account_hash, message):
    async with open_asyncio_connection(host, port) as rw_descriptor:
        reader, writer = rw_descriptor

        data = await reader.readline()
        logger.debug(data.decode())

        await authorise(reader, writer, account_hash)

        data = await reader.readline()
        logger.debug(data.decode())

        await submit_message(writer, message)
示例#4
0
async def read_msgs(host, port, queues):
    state = gui.ReadConnectionStateChanged
    queues['status_updates'].put_nowait(state.INITIATED)

    async with open_asyncio_connection(host, port) as rw_descriptor:
        reader, writer = rw_descriptor

        queues['status_updates'].put_nowait(state.ESTABLISHED)

        while True:
            data = await reader.readline()

            message = data.decode()
            queues['messages'].put_nowait(message)
            queues['saving'].put_nowait(message),
            queues['watchdog'].put_nowait('Connection is alive. Source: New message in chat')
async def register(host, port, nickname) -> dict:
    async with open_asyncio_connection(host, port) as rw_descriptor:
        reader, writer = rw_descriptor

        data = await reader.readline()
        logger.debug(data.decode())

        writer.write(b'\n')
        await writer.drain()

        data = await reader.readline()
        logger.debug(data.decode())

        nickname = sanitize(nickname)
        writer.writelines([nickname.encode(), b'\n'])
        await writer.drain()

        data = await reader.readline()
        logger.debug(data.decode())

        return json.loads(data)