Hi !
Thanks for the advice, I have been trying to implement websockets this week-end. It is a hard topic to me, I am making very slow progress... I got stuck on something, but I am not even sure I am going in the right direction.
So basically I need to send 60 'move' commands per second to my Repetier Server.
For now, I created the client side websocket responding to recv/send. But I am actually creating a websocket at each frame. Those have a timeout, but it is a mess anyway.
I didn't find a good solution to let the client websocket open for my stream of move commands.
But is it what I should do ?
Here is the code I am using:
class Communicate:
def
init(self, host, slug):
self.url = f'ws://{host}/socket?apikey={self.api}'
self.slug = slug
self.callback_id = 100 #init
self.th = None
self.ws = None
def _format_command(self, action="", data={}):
d = {"action":action, "data":data, "printer":self.slug, "callback_id":self.callback_id}
d = json.dumps(d)
self.callback_id += 1
return d
async def _get_message(self):
while True:
try:
msg = await self.ws.recv()
msg = json.loads(msg)
print(f'\nrecv: {msg}')
except websockets.exceptions.ConnectionClosed:
print('ConnectionClosed')
break
async def _move(self, data):
async def
send():
await self.ws.send(data)
async with websockets.connect(self.url) as self.ws:
res = await asyncio.gather(self.get_message(), _send())
return res
def send_coord(self, data):
""" :data: (json) {"x":x, "y":y, "z":z, "e":e, "speed":speed, "relative":relative} """
def
do_it():
r1, r2 = asyncio.run(self.move(data))
data = self._format_command(data)
self.th = threading.Thread(target=lambda: _do_it(), daemon=True)
self.th.start()
Thanks for your help :-)
thasor