heroku-python / flask-sockets

[DEPRECATED] Alternative: https://github.com/miguelgrinberg/flask-sock
MIT License
1.74k stars 164 forks source link

App and request context missing #19

Closed davidkhess closed 8 years ago

davidkhess commented 10 years ago

It would be nice if the app and request context were available when the handler gets called. Here's an alternative implementation of SocketMiddleware which does that:

    def __call__(self, environ, start_response):
        path = environ['PATH_INFO']

        if path in self.ws.url_map:
            handler = self.ws.url_map[path]
            websocket = environ['wsgi.websocket']

            with app.app_context():
                with current_app.request_context(environ):
                    handler(websocket)
        else:
            return self.app(environ, start_response)

This presumes that app (your Flask app) and current_app are imported from flask.

philipn commented 9 years ago

@davidkhess I've added a PR for this behavior above.

davidkhess commented 9 years ago

Looks good!

kennethreitz commented 8 years ago

This is now available!