Advertisement
Guest User

Untitled

a guest
Sep 25th, 2016
80
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.33 KB | None | 0 0
  1. import bge
  2. import asyncore, socket, json, os
  3.  
  4. HOST = 'localhost'
  5. SERVERPORT = 6000
  6. MESSAGE = ''
  7.  
  8. cont = bge.logic.getCurrentController()
  9. own = cont.owner
  10. sens = cont.sensors['Always']
  11. actu = cont.actuators['Messanger']
  12.  
  13. class HTTPClient(asyncore.dispatcher):
  14. #all methods below belong to the HTTPClient class but it is tedious to indent
  15. def __init__(self, host, port):
  16. asyncore.dispatcher.__init__(self)
  17. self.create_socket(socket.AF_INET, socket.SOCK_STREAM)
  18. try :
  19. self.connect( (host, port) )
  20. except:
  21. print("Unable to Connect") #not working as it should
  22. exit(1)
  23. data = " " #if data == 'quit()':
  24.  
  25. def handle_connect(self):
  26. pass
  27.  
  28. def handle_close(self):
  29. self.close()
  30.  
  31. def handle_read(self):
  32. data = self.recv(8192).decode('utf-8')#need to check for valid json
  33. data = json.loads(data)
  34. clas = data.get("class")
  35. #print(data.get(clas))
  36. MESSAGE = data.get(clas)
  37. if sens.positive:
  38. actu.propName = 'Cylinder'
  39. actu.subject = MESSAGE
  40. cont.activate(actu)
  41. else:
  42. cont.deactivate(actu)
  43.  
  44. def writable(self):
  45. return (len(self.buffer) > 0)
  46.  
  47. def handle_write(self):
  48. sent = self.send(self.buffer)
  49. self.buffer = self.buffer[sent:]
  50.  
  51.  
  52. client = HTTPClient(HOST, SERVERPORT)
  53. asyncore.loop()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement