Beispiel #1
0
class TCPConnection(QThread):
    connected = pyqtSignal(bool)
    message = pyqtSignal(str)

    def __init__(self, parent=None):
        super().__init__(parent)
        self.ip = "127.0.0.1"
        self.port = 4040
        self.username = None
        self.message_queue = Queue()
        self.client = TCPClient(self.ip, self.port, 2048)
        self.client.add_handler(ON_MESSAGE, self.message.emit)

    def connect(self):
        try:
            self.client.connect()
            return True
        except ConnectionRefusedError:
            return False

    def send_message(self, message):
        self.message_queue.put(f"{self.username}: {message}")

    def run(self):
        conn_status = self.connect()
        self.connected.emit(conn_status)
        if conn_status:
            self.client.send(f"{self.username} has joined!")
            self.client.start()
            while True:
                if not self.message_queue.empty():
                    self.client.send(self.message_queue.get())
Beispiel #2
0
from server import TCPServer
from client import TCPClient

import os
from time import sleep



if __name__ == "__main__":
    client = TCPClient('localhost', os.environ.get('PORT'))
    client.start()
    
    # time to create client
    sleep(1)
    
    for i in range(10):
        data = {'key': 'key'+str(i),'value': 'value'+str(i)}    
        client.sendData(data)
        sleep(5)