Пример #1
0
 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()
Пример #2
0
 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')
Пример #3
0
 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')
Пример #4
0
 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')
Пример #5
0
 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
Пример #6
0
 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)
Пример #7
0
 def send(self, msg):
     JPCProtocol(JPCProtocol.SEND, msg).send(self.server)
Пример #8
0
 def send_heartbeat(self):
     JPCProtocol(JPCProtocol.HEARTBEAT).send(self.server)