I'm running websocket-client on a raspberry pi to control a servo using data sent by a leap motion websocket server. The volume of data sent over the websocket connection by the leap motion is so high that there is a long delay with the pi catching up to the most recent message which only gets worse the longer it runs.
How can I discard all the old messages in a queue and just use the most recent one when checking? Currently I'm doing this:
ws = create_connection("ws://192.168.1.5:6437")
while True:
result = ws.recv()
resultj = json.loads(result)
if "hands" in resultj and len(resultj["hands"]) > 0:
# print resultj["hands"][0]["palmNormal"]
rotation = math.atan2(resultj["hands"][0]["palmNormal"][0], -resultj["hands"][0]["palmNormal"][1])
dc = translate(rotation, -1, 1, 5, 20)
pwm.ChangeDutyCycle(float(dc))