Merge pull request #12 from avatao-content/multi_websockets

Implement support for multiple websockets connecting to TFW
This commit is contained in:
Bokros Bálint 2018-02-19 10:08:40 +01:00 committed by GitHub
commit 654ddacb74
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -9,18 +9,24 @@ log = logging.getLogger(__name__)
class ZMQWebSocketHandler(WebSocketHandler): class ZMQWebSocketHandler(WebSocketHandler):
_event_handler_connector = EventHandlerConnector() _event_handler_connector = EventHandlerConnector()
instances = set()
def __init__(self, application, request, **kwargs): def prepare(self):
super().__init__(application, request, **kwargs) ZMQWebSocketHandler.instances.add(self)
def on_close(self):
ZMQWebSocketHandler.instances.remove(self)
def open(self, *args, **kwargs): def open(self, *args, **kwargs):
log.debug('WebSocket connection initiated') log.debug('WebSocket connection initiated')
self._event_handler_connector.register_callback(self.zmq_callback) self._event_handler_connector.register_callback(self.zmq_callback)
def zmq_callback(self, msg_parts): @staticmethod
def zmq_callback(msg_parts):
anchor, data = deserialize_all(*msg_parts) anchor, data = deserialize_all(*msg_parts)
log.debug('Received on pull socket: {}'.format(data)) log.debug('Received on pull socket: {}'.format(data))
self.write_message(data) for instance in ZMQWebSocketHandler.instances:
instance.write_message(data)
def on_message(self, message): def on_message(self, message):
log.debug('Received on WebSocket: {}'.format(message)) log.debug('Received on WebSocket: {}'.format(message))
@ -40,6 +46,7 @@ class FSMManagingSocketHandler(ZMQWebSocketHandler):
self.fsm.subscribe_message_handler(self.handle_fsm_message) self.fsm.subscribe_message_handler(self.handle_fsm_message)
def on_close(self): def on_close(self):
super().on_close()
self.fsm.unsubscribe_message_handler(self.handle_fsm_message) self.fsm.unsubscribe_message_handler(self.handle_fsm_message)
def handle_fsm_message(self, message): def handle_fsm_message(self, message):