-
-
Save alex-oleshkevich/80dab249a8fef9e47f1f82bed74d2c34 to your computer and use it in GitHub Desktop.
from django.urls import resolve | |
from .connection import WebSocket | |
def websockets(app): | |
async def asgi(scope, receive, send): | |
if scope["type"] == "websocket": | |
match = resolve(scope["raw_path"]) | |
await match.func(WebSocket(scope, receive, send), *match.args, **match.kwargs) | |
return | |
await app(scope, receive, send) | |
return asgi |
Also in middleware.py the return asgi
is on the same level as the function definition. It is kind of ambiguous to me what indentation needs to be fixed. Should return asgi
be indented in addition to await app(scope, receive, send)
or just the return?
Try now
ERROR: Exception in ASGI application Traceback (most recent call last): File "/usr/src/app/env/lib/python3.7/site-packages/uvicorn/protocols/http/httptools_impl.py", line 390, in run_asgi result = await app(self.scope, self.receive, self.send) File "/usr/src/app/env/lib/python3.7/site-packages/uvicorn/middleware/proxy_headers.py", line 45, in __call__ return await self.app(scope, receive, send) File "./shop/middleware.py", line 21, in asgi await app(scope, receive, send) File "/usr/src/app/env/lib/python3.7/site-packages/django/core/handlers/asgi.py", line 161, in __call__ response = await self.get_response_async(request) File "/usr/src/app/env/lib/python3.7/site-packages/django/core/handlers/base.py", line 148, in get_response_async response = await self._middleware_chain(request) TypeError: object HttpResponse can't be used in 'await' expression Exception in ASGI application Traceback (most recent call last): File "/usr/src/app/env/lib/python3.7/site-packages/uvicorn/protocols/http/httptools_impl.py", line 390, in run_asgi result = await app(self.scope, self.receive, self.send) File "/usr/src/app/env/lib/python3.7/site-packages/uvicorn/middleware/proxy_headers.py", line 45, in __call__ return await self.app(scope, receive, send) File "./shop/middleware.py", line 21, in asgi await app(scope, receive, send) File "/usr/src/app/env/lib/python3.7/site-packages/django/core/handlers/asgi.py", line 161, in __call__ response = await self.get_response_async(request) File "/usr/src/app/env/lib/python3.7/site-packages/django/core/handlers/base.py", line 148, in get_response_async response = await self._middleware_chain(request) TypeError: object HttpResponse can't be used in 'await' expression INFO: - "GET /favicon.ico HTTP/1.0" 500 Internal Server Error
That still doesnt work, because you are trying to call a coroutine in asgi.py
I've tried these things in asgi.py and still cannot get it to work: