示例#1
0
    def on_button_connect_clicked(self):
        client_name = self.userName.displayText()
        self.setWindowTitle('Chat client: ' + client_name)

        # TODO: Rewrite - user must enter password
        user_password = '******'

        self.chat_client = ChatClient('localhost', 5335, client_name)
        # Create TCP socket
        self.sock = socket(AF_INET, SOCK_STREAM)
        # Create connection with server
        self.chat_client.connect(self.sock)
        self.get_thread = GetMessagesThread(self.chat_client)
        # connect message_recieved signal with highlight_contact slot
        self.get_thread.message_received.connect(self.highlight_contact)
        if self.chat_client.check_presence(
        ) is True and self.chat_client.authenticate(user_password) is True:
            self.get_thread.start()
            contacts = self.chat_client.get_contacts()
            self.listWidgetContacts.addItems(contacts)
            for item in contacts:
                # first item should be 2 for skipping first item in contacts_current_item_changed function
                self.chats[item] = [(2, 0, '')]
        # set current item in contacts list
        items = self.listWidgetContacts.findItems(contacts[0], Qt.MatchExactly)
        if len(items) > 0:
            for item in items:
                self.listWidgetContacts.setCurrentItem(item)
                break
示例#2
0
class Chat(Frame):
    def __init__(self, master, controller):
        super().__init__(master)
        self.controller = controller
        self.msg = Label(self, font=('Ariel', 20), foreground='green')
        self.msg.pack()
        Button(self, text='End Chat', command=self.stop_chat).pack()

    def stop_chat(self):
        ask_server.stop(self.controller.username, 'call')

    def start_chat(self):
        user = self.controller.target
        if not user:
            user = self.controller.user_called
        self.msg['text'] = f'In chat with {user}'
        self.client = ChatClient()
        Thread(target=self.chat_ended, name='chat_ended', daemon=True).start()
        self.client.start()

    def chat_ended(self):
        time.sleep(2)
        while True:
            time.sleep(1)
            if not ask_server.is_in_chat(self.controller.username):
                self.client.end()
                self.controller.show_frame(Main)
                time.sleep(0.4)
                self.controller.frames[Called].start_checking()
                break
示例#3
0
 def start_chat(self):
     user = self.controller.target
     if not user:
         user = self.controller.user_called
     self.msg['text'] = f'In chat with {user}'
     self.client = ChatClient()
     Thread(target=self.chat_ended, name='chat_ended', daemon=True).start()
     self.client.start()
示例#4
0
def connect(host, port):
    chat_client = ChatClient(ip = host, port=port)
    loop = asyncio.get_event_loop()

    loop.run_until_complete(chat_client._connect())

    # display menu, wait for command from user, invoke method on client
    asyncio.ensure_future(handle_user_input(chat_client=chat_client, loop = loop))

    loop.run_forever()
示例#5
0
def connect(host, port):
    chat_client = ChatClient(ip=host, port=port)
    loop = asyncio.get_event_loop()

    loop.run_until_complete(chat_client._connect())

    twitter_blk_client = TwitterDMClient(consumer_key=consumer_key,
                                         consumer_secret=consumer_secret,
                                         access_token=access_token,
                                         access_secret=access_secret)
    try:
        twitter_blk_client.init_auth()
    except Exception as e:
        print('error authenticating with twitter API: {}'.format(e))
        # sys.exit(1)

    # display menu, wait for command from user, invoke method on client
    asyncio.ensure_future(
        handle_user_input(chat_client=chat_client,
                          twitter_client=twitter_blk_client,
                          loop=loop))
    asyncio.ensure_future(display_msgs(chat_client=chat_client))

    loop.run_forever()
示例#6
0
class MyWindow(QtWidgets.QMainWindow):
    def __init__(self, parent=None, user_name=None):
        QtWidgets.QWidget.__init__(self, parent)
        uic.loadUi('chat_gui.ui', self)
        self.pushButton.clicked.connect(QtWidgets.qApp.quit)
        self.setWindowTitle('Chat client: ' + user_name)

        self.sock = None
        self.chat_client = None
        self.get_thread = None

        self.chats = {}
        self.previous_contact_item = None

        self.pushButtonConnect.clicked.connect(self.on_button_connect_clicked)
        self.pushButtonSend.clicked.connect(self.on_button_send_clicked)
        self.pushButtonAddContact.clicked.connect(
            self.on_button_add_contact_clicked)
        self.pushButtonDeleteContact.clicked.connect(
            self.on_button_delete_contact_clicked)

        self.listWidgetContacts.currentItemChanged.connect(
            self.contacts_current_item_changed)

    def finished(self):
        self.get_thread.stop()
        self.sock.close()
        self.setGuiConnected(False)

    def on_button_connect_clicked(self):
        client_name = self.userName.displayText()
        self.setWindowTitle('Chat client: ' + client_name)

        # TODO: Rewrite - user must enter password
        user_password = '******'

        self.chat_client = ChatClient('localhost', 5335, client_name)
        # Create TCP socket
        self.sock = socket(AF_INET, SOCK_STREAM)
        # Create connection with server
        self.chat_client.connect(self.sock)
        self.get_thread = GetMessagesThread(self.chat_client)
        # connect message_recieved signal with highlight_contact slot
        self.get_thread.message_received.connect(self.highlight_contact)
        if self.chat_client.check_presence(
        ) is True and self.chat_client.authenticate(user_password) is True:
            self.get_thread.start()
            contacts = self.chat_client.get_contacts()
            self.listWidgetContacts.addItems(contacts)
            for item in contacts:
                # first item should be 2 for skipping first item in contacts_current_item_changed function
                self.chats[item] = [(2, 0, '')]
        # set current item in contacts list
        items = self.listWidgetContacts.findItems(contacts[0], Qt.MatchExactly)
        if len(items) > 0:
            for item in items:
                self.listWidgetContacts.setCurrentItem(item)
                break

    def on_button_send_clicked(self):
        message_text = self.lineEditMessage.displayText()
        if message_text != '':
            user_to = self.listWidgetContacts.currentItem().text()
            message_time = self.chat_client.send_jim_message(msg=message_text,
                                                             user_to=user_to)
            final_message = str(message_time) + ' ' + message_text
            self.listWidgetMessages.addItem(final_message)
            self.lineEditMessage.setText('')
            # write message to history list, first parameter 0 - local user, 1 - user_to
            if user_to in window.chats:
                logging.info('Message {} wrote to {} '.format(
                    message_text, user_to))
                window.chats[user_to].append((0, message_time, message_text))
            else:
                # Received first message from user_from
                window.chats[user_to] = ((0, message_time, message_text), )

    def on_button_add_contact_clicked(self):
        contact_name = self.addContactName.displayText()
        if contact_name != '':
            result = self.chat_client.add_contact(contact_name)
            if result is True:
                self.listWidgetContacts.addItem(contact_name)
                # create history for this contact
                self.chats[contact_name] = [(2, 0, '')]
            else:
                logging.info('Add contact return False')
            self.addContactName.clear()

    def on_button_delete_contact_clicked(self):
        contact_name = self.listWidgetContacts.currentItem().text()
        if contact_name != '':
            result = self.chat_client.del_contact(contact_name)
            if result is True:
                self.listWidgetContacts.takeItem(
                    self.listWidgetContacts.row(
                        self.listWidgetContacts.currentItem()))
            else:
                logging.info('Delete contact return False')
        pass

    def contacts_current_item_changed(self):
        user_to = self.listWidgetContacts.currentItem().text()
        logging.info('current item: {}'.format(user_to))
        if self.previous_contact_item and self.previous_contact_item != user_to:
            self.listWidgetMessages.clear()
            for item in self.chats[user_to]:
                if item[0] is 0:
                    final_message = str(item[1]) + ' ' + item[2]
                else:
                    final_message = str(
                        item[1]) + ' ' + user_to + ' > ' + item[2]
                if item[0] != 2:
                    self.listWidgetMessages.addItem(final_message)
                logging.info(item)
            normal_font = QtGui.QFont()
            normal_font.setBold(False)
            self.listWidgetContacts.currentItem().setFont(normal_font)

        self.previous_contact_item = user_to

    def highlight_contact(self, user_from):
        """
        Finds item by name in contact list and highlights it.
        Use this method for new messages.
        :param user_from: Contact name
        :return:
        """
        logging.info('change background in list')
        items = self.listWidgetContacts.findItems(user_from, Qt.MatchExactly)
        if len(items) > 0:
            for item in items:
                # item.setBackground(QtGui.QColor(UNREAD_COLOR))
                bold_font = QtGui.QFont()
                bold_font.setBold(True)
                item.setFont(bold_font)
示例#7
0
from client.chat_client import ChatClient
from client.console_chat import ConsoleChat

console_chat = ConsoleChat(ChatClient(5000, '127.0.0.1'))
console_chat.start()
示例#8
0
def echo_client():
    print('Hello. This is chat app. Please use next command:')
    print('1. ch_receiver - for changing interlocutor name')
    print('2. add_contact - for adding new existing contact to contact list')
    print('3. get_contacts - for printing contact list')
    print('4. del_contact - for deleting certain contact from contact list')
    print('5. exit - for closing app')
    print('Start your conversation:')

    user_name = input('Enter your nickname: ')

    # TODO: Rewrite - user must enter password
    user_password = '******'

    user_friend = change_receiver()

    chat_client = ChatClient('localhost', 5335, user_name)
    # Create TCP socket
    with socket(AF_INET, SOCK_STREAM) as sock:
        # Create connection with server
        chat_client.connect(sock)
        get_thread = GetMessagesThread(chat_client)

        # TODO: Rewrite - add registration for user
        if chat_client.check_presence() is True and chat_client.authenticate(user_password) is True:
            get_thread.start()
            while True:
                msg = input()
                if msg == 'exit':
                    break
                elif msg == 'ch_receiver':
                    user_friend = change_receiver()
                elif msg == 'get_contacts':
                    print(chat_client.get_contacts())
                elif msg == 'add_contact':
                    contact = input('Enter contact: ')
                    chat_client.add_contact(contact)
                elif msg == 'del_contact':
                    contact = input('Enter contact for deleting: ')
                    chat_client.del_contact(contact)
                else:
                    chat_client.send_jim_message(VALUE_MESSAGE, msg, user_friend)
            get_thread.stop()
            chat_client.disconnect()