def __init__(self, server_address): self.server = socket.socket(socket.AF_INET, socket.SOCK_STREAM) self.server.connect((server_address, JPCProtocol.STANDARD_PORT)) JPCProtocol(JPCProtocol.HELLO).send(self.server) self.gui = JPCClientGUI() threading.Thread(target=self.run).start() self.gui.run()
def tx_rx_heartbeats(self): t = time.time() while True: n = time.time() if n - t >= JPCProtocol.HEARTBEAT_INTERVAL: t = n for user in self.users: if user.connected: JPCProtocol(JPCProtocol.HEARTBEAT).send(user.connection) if t - user.last_heartbeat >= JPCProtocol.HEARTBEAT_TIMEOUT: print('died')
def handle(self, connection): running = True try: while running: data = connection.recv(64000) if data: data_list = JPCProtocol.decode(data) for json_data in data_list: print(json_data) self.process(json_data, connection) except ConnectionAbortedError: print('Connection Aborted')
def run(self): try: threading.Thread(target=self.send_heartbeats).start() running = True while running: data = self.server.recv(64000) if data: data_list = JPCProtocol.decode(data) for item in data_list: running = self.process(item) except ConnectionResetError: print('Connection Reset')
def close(self, opcode=JPCProtocol.CLOSE, payload=None): JPCProtocol(opcode, payload).send(self.connection) self.connection.close() self.connection = None self.last_heartbeat = None self.connected = False
def send_message(self, message, recipient): user = self.get_by_name(recipient) if user and user.connected: packet = JPCProtocol(JPCProtocol.TELL, {'recipient': recipient, 'message': message}) user.send(packet)
def send(self, msg): JPCProtocol(JPCProtocol.SEND, msg).send(self.server)
def send_heartbeat(self): JPCProtocol(JPCProtocol.HEARTBEAT).send(self.server)