Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

generate a request context for all the WS handler exec environments #77

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 7 additions & 3 deletions flask_uwsgi_websocket/_asyncio.py
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down
5 changes: 4 additions & 1 deletion flask_uwsgi_websocket/_gevent.py
Original file line number Diff line number Diff line change
Expand Up @@ -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):
Expand Down
5 changes: 3 additions & 2 deletions flask_uwsgi_websocket/websocket.py
Original file line number Diff line number Diff line change
Expand Up @@ -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 []


Expand Down