import logging from tornado.websocket import WebSocketHandler from buttons import Buttons from util import parse_anchor_from_message import component_connector class ZMQWebSocketHandler(WebSocketHandler): def __init__(self, application, request, **kwargs): super().__init__(application, request, **kwargs) self.fsm = Buttons(self) def open(self, *args, **kwargs): def zmq_callback(msg_parts): anchor, data = msg_parts logging.debug('Received on pull socket: {}'.format(data.decode())) self.write_message(data.decode()) component_connector.register_callback(zmq_callback) def on_message(self, message): logging.debug('Received on WebSocket: {}'.format(message)) self.fsm.trigger(self._parse_anchor(message), message=message) def send_message(self, message: str, anchor: str = None): component_connector.send_message(message, anchor) def on_close(self): pass # much secure, very cors, wow def check_origin(self, origin): return True