19 import tornado.websocket
20 from pyee
import EventEmitter
26 EventBusEmitter = EventEmitter()
28 client_connections = []
32 def __init__(self, application, request, **kwargs):
33 tornado.websocket.WebSocketHandler.__init__(
34 self, application, request, **kwargs)
37 def on(self, event_name, handler):
38 self.emitter.on(event_name, handler)
43 deserialized_message = Message.deserialize(message)
48 self.emitter.emit(deserialized_message.type, deserialized_message)
49 except Exception
as e:
51 traceback.print_exc(file=sys.stdout)
54 for client
in client_connections:
55 client.write_message(message)
58 self.write_message(
Message(
"connected").serialize())
59 client_connections.append(self)
62 client_connections.remove(self)
64 def emit(self, channel_message):
65 if (hasattr(channel_message,
'serialize')
and 66 callable(getattr(channel_message,
'serialize'))):
67 self.write_message(channel_message.serialize())
69 self.write_message(json.dumps(channel_message))
def __init__(self, application, request, kwargs)
def check_origin(self, origin)
def emit(self, channel_message)
def on_message(self, message)
def on(self, event_name, handler)