def run_with_id(self, printer_id):
"""
Runs thread, which listens on socket.
Executes given callbacks on events
"""
def on_message(ws, data):
if data.startswith('m'):
self.on_message(ws, json.loads(data[1:]))
elif data.startswith('a'):
for msg in json.loads(data[1:]):
self.on_message(ws, msg, printer_id)
def on_error(ws, exception):
data = {
"id": printer_id,
"state": {
"text": "Offline/Unreachable"
}
}
socketio.emit("status", data, room=str(printer_id))
self.socket = websocket.WebSocketApp(self.url,
on_open=self.on_open,
on_close=self.on_close,
on_message=on_message,
on_error=on_error)
self.thread = Thread(target=self.run_forever)
self.thread.daemon = True
self.thread.start()
评论列表
文章目录