From 24fd753679fffe5a579cb7399de32eca7be76571 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Krist=C3=B3f=20T=C3=B3th?= Date: Fri, 16 Feb 2018 17:47:30 +0100 Subject: [PATCH] Implement support for multiple websockets connecting to TFW --- lib/tfw/networking/server/zmq_websocket_handler.py | 14 ++++++++++++-- 1 file changed, 12 insertions(+), 2 deletions(-) diff --git a/lib/tfw/networking/server/zmq_websocket_handler.py b/lib/tfw/networking/server/zmq_websocket_handler.py index 35d5fec..31cd0a2 100644 --- a/lib/tfw/networking/server/zmq_websocket_handler.py +++ b/lib/tfw/networking/server/zmq_websocket_handler.py @@ -9,6 +9,13 @@ log = logging.getLogger(__name__) class ZMQWebSocketHandler(WebSocketHandler): _event_handler_connector = EventHandlerConnector() + instances = set() + + def prepare(self): + ZMQWebSocketHandler.instances.add(self) + + def on_close(self): + ZMQWebSocketHandler.instances.remove(self) def __init__(self, application, request, **kwargs): super().__init__(application, request, **kwargs) @@ -17,10 +24,12 @@ class ZMQWebSocketHandler(WebSocketHandler): log.debug('WebSocket connection initiated') 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) 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): log.debug('Received on WebSocket: {}'.format(message)) @@ -40,6 +49,7 @@ class FSMManagingSocketHandler(ZMQWebSocketHandler): self.fsm.subscribe_message_handler(self.handle_fsm_message) def on_close(self): + super().on_close() self.fsm.unsubscribe_message_handler(self.handle_fsm_message) def handle_fsm_message(self, message):