Esempio n. 1
0
    def toggle_connection(self):
        if not self.connected:
            try:
                self.chat_window.put_info("Connecting")
                self.server = socket.create_connection(self.settings_bar.get_address())
            except ConnectionRefusedError:
                self.chat_window.put_error("Error: Connection refused")
            except (ValueError, socket.gaierror):
                self.chat_window.put_error("Error: Address not valid")
            except TimeoutError:
                self.chat_window.put_error("Error: Timeout")
            else:
                self.chat_window.put_info("Connection established")
                ip, port = self.server.getpeername()
                self.chat_window.put_info(str(ip) + ":" + str(port))
                self.chat_window.put_separator()

                self.connected = True
                self.server.setblocking(False)
                self.settings_bar.lock()
                self.text_input.unlock()

                prot.put_tuple(self.server, ("Name", self.settings_bar.get_name()))

                self._root().after(1, self.receive_msg)
        else:
            self.server.close()
            self.chat_window.put_separator()
            self.chat_window.put_info("Disconnected")
            self.online_bar.update_names([])
            self.connected = False
            self.settings_bar.unlock()
            self.text_input.lock()
        return
Esempio n. 2
0
def parse(message, s):
    if message[0] == "Name":
        names[s] = message[1]
        print(s.getpeername(), "is now called:", names[s])
        broadcast("Online")
        broadcast_tuple(names.values())
    elif message[0] == "Mess":
        if message[1] is None:
            message = ("Mess", '')
        print(s.getpeername(), names[s], ":", message[1])
        for conn in connections:
            if conn != s:
                prot.put_tuple(conn, ("Mess", names[s], message[1]))
    return
Esempio n. 3
0
 def send_msg(self, event):
     if self.connected and self.text_input.get_message() != '':
         prot.put_tuple(self.server, ("Mess", self.text_input.get_message()))
         self.chat_window.put_message(self.settings_bar.get_name(), self.text_input.get_message())
         self.text_input.message.set('')
     return
Esempio n. 4
0
def broadcast_tuple(message):
    for conn in connections:
        prot.put_tuple(conn, message)