diff --git a/Client.py b/Client.py new file mode 100644 index 0000000..d7791c4 --- /dev/null +++ b/Client.py @@ -0,0 +1,33 @@ +#!/usr/bin/python +import socket +import threading +import re +# Define server ip and port +ip = '127.0.0.1' +port = 2222 +#Define globals +decodedServerData = "" +def recvData(ip, port): + global decodedServerData + # Connect to Server + client = socket.socket(socket.AF_INET, socket.SOCK_STREAM) + #client.setblocking(False) + client.connect((ip, port)) + while True: + from_server = client.recv(4096) + #Decode Sever message + decodedServerData = from_server.decode() + +def printData(): + while True: + global decodedServerData + #print("Raw Data:", decodedServerData) + #print('\n'+":O", end="\r", flush=True) + +# Execute all functions as threads +if __name__ == "__main__": + #Create Threads + t1 = threading.Thread(target=recvData, args=(ip, port)) + t2 = threading.Thread(target=printData, args=()) +t1.start() +t2.start() diff --git a/Server.py b/Server.py new file mode 100644 index 0000000..fb367a7 --- /dev/null +++ b/Server.py @@ -0,0 +1,51 @@ +#!/usr/bin/python +# Import libraries +from inputs import devices +from inputs import get_gamepad +import threading +import socket +from termcolor import colored +# Define ip/port to use +ip = "127.0.0.1" +port = 2222 +# Show available gamepads +print("Gamepads available:") +print(devices.gamepads) +#Define globals +controllerData = "" +# Show device output, send it in var +#def printInput(): + #global controllerData +# Capture device input and send it via a web socket +#def inputCapture(ip, port): + #global controllerData +with socket.socket(socket.AF_INET, socket.SOCK_STREAM) as s: + s.bind((ip, port)) + s.listen(1) + #s.setblocking(False) + conn, addr = s.accept() + with conn: + print(colored('Connected by', 'red'), addr) + while True: + events = get_gamepad() + for event in events: + controllerDataTuple = event.ev_type, event.code, event.state + controllerData = str(controllerDataTuple) + print(controllerData) + #Encode datastream for transmittion + encodedControllerData=controllerData.encode() + conn.send(encodedControllerData) + #Wait for ok signal from client before continuing... + + + + + + +# Execute all functions as threads +#if __name__ == "__main__": + #Create Threads + #t1 = threading.Thread(target=printInput, args=()) + #t2 = threading.Thread(target=inputCapture, args=(ip, port)) +#t1.start() +#t2.start()