class ConsoleChatClient:
    def __init__(self, port, host):
        self.username: str = ''
        self._client = ChatClient(port, host)

    def start(self):
        self._get_username()
        self._client.start_listen_messages(self._message_received)
        self._client.start_pingpong()
        self._client.on_server_unavailable = self._chat_server_unavailable
        self._get_inputs()

    def _get_username(self):
        while not self.username:
            self.username = input('Enter Username: '******'[{note.name}]: {note.message}\n')

    def _chat_server_unavailable(self):
        print("chat server is unavailable")

    def _get_inputs(self):
        try:
            text = input('> ')
            while text != 'quit':
                if text:
                    self._client.send_message(self.username, text)
                text = input('> ')
        except KeyboardInterrupt:
            pass
        print('Bye')
        # We tell the customer to disconnect from the server.
        # In general, it is not necessary, because our program is already ending at this point
        self._client.close()
예제 #2
0
def chat_client_thread(port, pub):
    print("[Client] Public: ", pub)
    exchange_client = ExchangeClient(port, pub)
    sym_key = chat_client_gen_sym_key()
    print("[Client] sym_key: ", sym_key)
    exchange_client.send_message(sym_key)

    chat_client = ChatClient(port, sym_key)
    while True:
        print('Send> ', end='')
        msg = input()
        chat_client.send_message(msg)