79 lines
2.3 KiB
Python
79 lines
2.3 KiB
Python
# Copyright 2016 Mycroft AI, Inc.
|
|
#
|
|
# This file is part of Mycroft Core.
|
|
#
|
|
# Mycroft Core is free software: you can redistribute it and/or modify
|
|
# it under the terms of the GNU General Public License as published by
|
|
# the Free Software Foundation, either version 3 of the License, or
|
|
# (at your option) any later version.
|
|
#
|
|
# Mycroft Core is distributed in the hope that it will be useful,
|
|
# but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
# GNU General Public License for more details.
|
|
#
|
|
# You should have received a copy of the GNU General Public License
|
|
# along with Mycroft Core. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
|
|
import json
|
|
import sys
|
|
import traceback
|
|
|
|
import tornado.websocket
|
|
from pyee import EventEmitter
|
|
|
|
import mycroft.util.log
|
|
from mycroft.messagebus.message import Message
|
|
|
|
logger = mycroft.util.log.getLogger(__name__)
|
|
__author__ = 'seanfitz'
|
|
|
|
EventBusEmitter = EventEmitter()
|
|
|
|
client_connections = []
|
|
|
|
|
|
class WebsocketEventHandler(tornado.websocket.WebSocketHandler):
|
|
def __init__(self, application, request, **kwargs):
|
|
tornado.websocket.WebSocketHandler.__init__(
|
|
self, application, request, **kwargs)
|
|
self.emitter = EventBusEmitter
|
|
|
|
def on(self, event_name, handler):
|
|
self.emitter.on(event_name, handler)
|
|
|
|
def on_message(self, message):
|
|
logger.debug(message)
|
|
try:
|
|
deserialized_message = Message.deserialize(message)
|
|
except:
|
|
return
|
|
|
|
try:
|
|
self.emitter.emit(deserialized_message.type, deserialized_message)
|
|
except Exception, e:
|
|
logger.exception(e)
|
|
traceback.print_exc(file=sys.stdout)
|
|
pass
|
|
|
|
for client in client_connections:
|
|
client.write_message(message)
|
|
|
|
def open(self):
|
|
self.write_message(Message("connected").serialize())
|
|
client_connections.append(self)
|
|
|
|
def on_close(self):
|
|
client_connections.remove(self)
|
|
|
|
def emit(self, channel_message):
|
|
if (hasattr(channel_message, 'serialize') and
|
|
callable(getattr(channel_message, 'serialize'))):
|
|
self.write_message(channel_message.serialize())
|
|
else:
|
|
self.write_message(json.dumps(channel_message))
|
|
|
|
def check_origin(self, origin):
|
|
return True
|