Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import bge
- import asyncore, socket, json, os
- HOST = 'localhost'
- SERVERPORT = 6000
- MESSAGE = ''
- cont = bge.logic.getCurrentController()
- own = cont.owner
- sens = cont.sensors['Always']
- actu = cont.actuators['Messanger']
- class HTTPClient(asyncore.dispatcher):
- #all methods below belong to the HTTPClient class but it is tedious to indent
- def __init__(self, host, port):
- asyncore.dispatcher.__init__(self)
- self.create_socket(socket.AF_INET, socket.SOCK_STREAM)
- try :
- self.connect( (host, port) )
- except:
- print("Unable to Connect") #not working as it should
- exit(1)
- data = " " #if data == 'quit()':
- def handle_connect(self):
- pass
- def handle_close(self):
- self.close()
- def handle_read(self):
- data = self.recv(8192).decode('utf-8')#need to check for valid json
- data = json.loads(data)
- clas = data.get("class")
- #print(data.get(clas))
- MESSAGE = data.get(clas)
- if sens.positive:
- actu.propName = 'Cylinder'
- actu.subject = MESSAGE
- cont.activate(actu)
- else:
- cont.deactivate(actu)
- def writable(self):
- return (len(self.buffer) > 0)
- def handle_write(self):
- sent = self.send(self.buffer)
- self.buffer = self.buffer[sent:]
- client = HTTPClient(HOST, SERVERPORT)
- asyncore.loop()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement