Last active
August 29, 2015 14:08
-
-
Save adoc/17053accfb0e190df9c4 to your computer and use it in GitHub Desktop.
Pyramid Socket IO - interesting way to serve with a dynamic namespace. (includes pyramid.route magics)
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
from pyramid.interfaces import IRoutesMapper | |
from pyramid.request import Request | |
@view_config(route_name='socket_endpoint', renderer='json') | |
def socket_endpoint(request): | |
"""Create a socket endpoint for the client. | |
""" | |
if 'ns' in request.params: | |
# To match an arbitrary route from ``request.params['ns']``. | |
ns = request.params['ns'] | |
dummy_request = Request.blank(ns) | |
routes_mapper = request.registry.queryUtility(IRoutesMapper) | |
mapper = routes_mapper(dummy_request) | |
route, match = mapper['route'], mapper['match'] | |
if route and route.name == 'ions_engine_namespace': | |
engine_id = match['engine_id'] | |
return socketio.socketio_manage(request.environ, | |
{ns: EngineSpace}, | |
request=request) | |
return {} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// http://stackoverflow.com/a/19618408 | |
var connect = function (ns, opts) { | |
opts = _.extend(opts || {}, {query: 'ns='+ns}); | |
return io.connect(ns, opts); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment