diff --git a/mopidy/frontends/http/__init__.py b/mopidy/frontends/http/__init__.py index 7294b40d..61aa5625 100644 --- a/mopidy/frontends/http/__init__.py +++ b/mopidy/frontends/http/__init__.py @@ -10,11 +10,12 @@ from mopidy.core import CoreListener try: import cherrypy from ws4py.server.cherrypyserver import WebSocketPlugin, WebSocketTool - from ws4py.websocket import WebSocket from ws4py.messaging import TextMessage except ImportError as import_error: raise exceptions.OptionalDependencyError(import_error) +from . import ws + logger = logging.getLogger('mopidy.frontends.http') @@ -41,10 +42,12 @@ class HttpFrontend(pykka.ThreadingActor, CoreListener): cherrypy.tools.websocket = WebSocketTool() def _create_app(self): - return cherrypy.tree.mount(Root(self.core), '/', { + root = Root(self.core) + root.ws = ws.WebSocketResource() + return cherrypy.tree.mount(root, '/', { '/ws': { 'tools.websocket.on': True, - 'tools.websocket.handler_cls': EventWebSocketHandler, + 'tools.websocket.handler_cls': ws.WebSocketHandler, }, }) @@ -72,25 +75,6 @@ class HttpFrontend(pykka.ThreadingActor, CoreListener): old_state, new_state))) -class EventWebSocketHandler(WebSocket): - def opened(self): - remote = cherrypy.request.remote - logger.debug(u'New WebSocket connection from %s:%d', - remote.ip, remote.port) - - def closed(self, code, reason=None): - remote = cherrypy.request.remote - logger.debug(u'Closed WebSocket connection from %s:%d ' - 'with code %s and reason %r', - remote.ip, remote.port, code, reason) - - def received_message(self, message): - remote = cherrypy.request.remote - logger.debug(u'Received WebSocket message from %s:%d: %s', - remote.ip, remote.port, message) - # This is where we would handle incoming messages from the clients - - class Root(object): def __init__(self, core): self.core = core @@ -106,7 +90,3 @@ class Root(object): 'playback_state': playback_state, 'current_track': track, } - - @cherrypy.expose - def ws(self): - logger.debug(u'WebSocket handler created') diff --git a/mopidy/frontends/http/ws.py b/mopidy/frontends/http/ws.py new file mode 100644 index 00000000..53ad651d --- /dev/null +++ b/mopidy/frontends/http/ws.py @@ -0,0 +1,37 @@ +import logging + +from mopidy import exceptions + +try: + import cherrypy + from ws4py.websocket import WebSocket +except ImportError as import_error: + raise exceptions.OptionalDependencyError(import_error) + + +logger = logging.getLogger('mopidy.frontends.http') + + +class WebSocketResource(object): + @cherrypy.expose + def index(self): + logger.debug(u'WebSocket handler created') + + +class WebSocketHandler(WebSocket): + def opened(self): + remote = cherrypy.request.remote + logger.debug(u'New WebSocket connection from %s:%d', + remote.ip, remote.port) + + def closed(self, code, reason=None): + remote = cherrypy.request.remote + logger.debug(u'Closed WebSocket connection from %s:%d ' + 'with code %s and reason %r', + remote.ip, remote.port, code, reason) + + def received_message(self, message): + remote = cherrypy.request.remote + logger.debug(u'Received WebSocket message from %s:%d: %s', + remote.ip, remote.port, message) + # This is where we would handle incoming messages from the clients