http: Move web socket code to new module
This commit is contained in:
parent
5b8f391bc7
commit
37ab7c766d
@ -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')
|
||||
|
||||
37
mopidy/frontends/http/ws.py
Normal file
37
mopidy/frontends/http/ws.py
Normal file
@ -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
|
||||
Loading…
Reference in New Issue
Block a user