TheImperial

Untitled

Jul 26th, 2021
839
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. def emit(event,message,client):
  2.     global splitter
  3.     global encode_decode_type
  4.  
  5.     try:
  6.         temp_data = {}
  7.         temp_data[event] = message
  8.         temp_data['identify'] = event
  9.         message = json.dumps(temp_data) + splitter
  10.         client.sendall(message.encode(encode_decode_type))
  11.     except:
  12.         pass
  13.    
  14. def handle_client(connection,addr):
  15.     global defaultsize
  16.     global encode_decode_type
  17.     global player_debug
  18.     global splitter
  19.     global players
  20.  
  21.     data_collection = []
  22.     connected = True
  23.  
  24.     while connected:
  25.         try:
  26.             message = str(connection.recv(100000),encode_decode_type)
  27.  
  28.             if player_debug == 1:
  29.                 print('Message: ' + message)
  30.  
  31.             if len(message) > 0:
  32.                 data_collection = message.split(splitter)
  33.                 data_collection = list(filter(None,data_collection))
  34.  
  35.                 if player_debug == 1:
  36.                     print('Data Collection: ' + str(data_collection))
  37.  
  38.                 if 'disconnect' in message:
  39.                     remove_player(addr[0])
  40.                     connected = False
  41.                     break
  42.  
  43.                 for data in data_collection:
  44.                     if player_debug == 1:
  45.                         print('Data: ' + data)
  46.  
  47.                     try:
  48.                         temp_data = json.loads(data)
  49.  
  50.                         if player_debug == 1:
  51.                             print('Temp Data: ' + str(temp_data))
  52.  
  53.                         for keys in temp_data.keys():
  54.                             if keys in use:
  55.                                 threading.Thread(target=use[keys],args=[connection,addr,temp_data[keys]]).start()
  56.                     except:
  57.                         pass
  58.         except ConnectionError:
  59.             remove_player(addr[0])
  60.             connected = False
  61.  
  62.     if type(connection) == socket.socket:
  63.         remove_player(addr[0])
  64.         connection.close()
  65.        
  66. def start(host,port):
  67.     global server
  68.     global players
  69.  
  70.     server = socket.socket()
  71.     server.bind((host,port))
  72.     server.listen()
  73.    
  74.     while True:
  75.         connection, addr = server.accept()
  76.         player = {'socket': connection,'addr': addr[0]}
  77.  
  78.         if player_exists(addr[0]) == False:
  79.             add_player(player,addr[0])
  80.             threading.Thread(target=handle_client,args=(connection,addr)).start()
  81.  
  82.             if 'connect' in use:
  83.                 use['connect'](connection,addr)
RAW Paste Data