diff --git a/flask_uwsgi_websocket/_asyncio.py b/flask_uwsgi_websocket/_asyncio.py index 2be5aec..d8c03a3 100644 --- a/flask_uwsgi_websocket/_asyncio.py +++ b/flask_uwsgi_websocket/_asyncio.py @@ -116,13 +116,17 @@ def __call__(self, environ, start_response): if not handler or 'HTTP_SEC_WEBSOCKET_KEY' not in environ: return self.wsgi_app(environ, start_response) + assert asyncio.iscoroutinefunction(handler) uwsgi.websocket_handshake(environ['HTTP_SEC_WEBSOCKET_KEY'], environ.get('HTTP_ORIGIN', '')) client = self.client(environ, uwsgi.connection_fd(), self.websocket.timeout, greenlet.getcurrent()) - - assert asyncio.iscoroutinefunction(handler) - asyncio.Task(asyncio.coroutine(handler)(client, **args)) + + async def call_handler(): + with self.websocket.app.request_context(environ): + await handler(client, **args) + asyncio.Task(call_handler()) + f = GreenFuture() asyncio.Task(client._send_ready(f)) try: diff --git a/flask_uwsgi_websocket/_gevent.py b/flask_uwsgi_websocket/_gevent.py index 5440be7..59b5f95 100644 --- a/flask_uwsgi_websocket/_gevent.py +++ b/flask_uwsgi_websocket/_gevent.py @@ -75,7 +75,10 @@ def __call__(self, environ, start_response): self.websocket.timeout) # spawn handler - handler = spawn(handler, client, **args) + def wrapped_handler(): + with self.websocket.app.request_context(environ): + return handler(client, **args) + handler = spawn(wrapped_handler) # spawn recv listener def listener(client): diff --git a/flask_uwsgi_websocket/websocket.py b/flask_uwsgi_websocket/websocket.py index 248d56d..9b372ab 100644 --- a/flask_uwsgi_websocket/websocket.py +++ b/flask_uwsgi_websocket/websocket.py @@ -69,9 +69,10 @@ def __call__(self, environ, start_response): if not handler or 'HTTP_SEC_WEBSOCKET_KEY' not in environ: return self.wsgi_app(environ, start_response) - uwsgi.websocket_handshake(environ['HTTP_SEC_WEBSOCKET_KEY'], environ.get('HTTP_ORIGIN', '')) - handler(self.client(environ, uwsgi.connection_fd(), self.websocket.timeout), **args) + app = self.websocket.app + with app.request_context(environ): + handler(self.client(environ, uwsgi.connection_fd(), self.websocket.timeout), **args) return []