def on_message(self, raw_message): logging.info(raw_message) message = utils.loads(raw_message) request_id = message.get("request_id") method = message["method"] args = message.get("args") try: if method == "subscribe": self.flinger.subscribe(self,args.get("name")) elif method == "unsubscribe": self.flinger.unsubscribe(self,args.get("name")) elif method == "broadcast": self.flinger.broadcast(args["message"],args.get("options")) elif method == "handle": request = Request(args["name"],args.get("request_options"), self.handled, request_id) self.requests.append(request) self.flinger.request_response(request, args.get("options")) elif method == "handled": callback = self.callbacks.get(request_id) if callback: del self.callbacks[request_id] if message.get("result"): callback.result = message["result"] else: callback.error = message["error"] if self.worker is True: self.flinger.subscribe(self,callback.name) except: logging.exception(raw_message)
def _on_message(self, raw_message): logging.info(raw_message) message = utils.loads(raw_message) if message.get("response_id"): request = self._requests.get(message.get("response_id")) if request: if message.get("result"): request.set_result(message["result"]) else: request.set_exception(message["error"]) elif message.get("request_id"): response = {"request_id": message["request_id"], "method": "handled"} try: response['result']=self.requested(message["name"], **message.get("options")) except Exception as ex: response["error"] = str(ex) logging.info(response) self.write_message(response) else: self.listen(message.get("message"), message.get("options"))