mirror of
https://github.com/avatao-content/baseimage-tutorial-framework
synced 2024-11-13 01:27:17 +00:00
23 lines
641 B
Python
23 lines
641 B
Python
from tfw.internals.networking import Scope
|
|
|
|
|
|
class TFWRouter:
|
|
def __init__(self, send_to_zmq, send_to_websockets):
|
|
self.send_to_zmq = send_to_zmq
|
|
self.send_to_websockets = send_to_websockets
|
|
|
|
def route(self, message):
|
|
scope = Scope(message.pop('scope', 'zmq'))
|
|
|
|
routing_table = {
|
|
Scope.ZMQ: self.send_to_zmq,
|
|
Scope.WEBSOCKET: self.send_to_websockets,
|
|
Scope.BROADCAST: self.broadcast
|
|
}
|
|
action = routing_table[scope]
|
|
action(message)
|
|
|
|
def broadcast(self, message):
|
|
self.send_to_zmq(message)
|
|
self.send_to_websockets(message)
|