python - 应用程序内的达芙妮 Django 频道异常:('不支持的 URI 方案','http')
问题描述
我刚刚开始测试在Django Channels和Redis上实现WebSockets。我有一个 redis 服务器正在运行,后端正在运行:(所有 mysite 都已更改)http://localhost:6379
daphne -b 0 -p 8000 mysite.asgi:application
每当我http://localhost:8000/ws/login
在前端访问时,我都会收到此错误:
Exception inside application: ('Unsupported URI scheme', 'http')
Traceback (most recent call last):
File "/opt/homebrew/lib/python3.9/site-packages/channels/routing.py", line 71, in __call__
return await application(scope, receive, send)
File "/opt/homebrew/lib/python3.9/site-packages/channels/sessions.py", line 47, in __call__
return await self.inner(dict(scope, cookies=cookies), receive, send)
File "/opt/homebrew/lib/python3.9/site-packages/channels/sessions.py", line 254, in __call__
return await self.inner(wrapper.scope, receive, wrapper.send)
File "/opt/homebrew/lib/python3.9/site-packages/channels/auth.py", line 181, in __call__
return await super().__call__(scope, receive, send)
File "/opt/homebrew/lib/python3.9/site-packages/channels/middleware.py", line 26, in __call__
return await self.inner(scope, receive, send)
File "/opt/homebrew/lib/python3.9/site-packages/channels/routing.py", line 150, in __call__
return await application(
File "/opt/homebrew/lib/python3.9/site-packages/channels/consumer.py", line 94, in app
return await consumer(scope, receive, send)
File "/opt/homebrew/lib/python3.9/site-packages/channels/consumer.py", line 58, in __call__
await await_many_dispatch(
File "/opt/homebrew/lib/python3.9/site-packages/channels/utils.py", line 58, in await_many_dispatch
await task
File "/opt/homebrew/lib/python3.9/site-packages/channels/utils.py", line 50, in await_many_dispatch
result = task.result()
File "/opt/homebrew/lib/python3.9/site-packages/channels_redis/core.py", line 468, in receive
message_channel, message = await self.receive_single(
File "/opt/homebrew/lib/python3.9/site-packages/channels_redis/core.py", line 523, in receive_single
content = await self._brpop_with_clean(
File "/opt/homebrew/lib/python3.9/site-packages/channels_redis/core.py", line 356, in _brpop_with_clean
async with self.connection(index) as connection:
File "/opt/homebrew/lib/python3.9/site-packages/channels_redis/core.py", line 884, in __aenter__
self.conn = await self.pool.pop()
File "/opt/homebrew/lib/python3.9/site-packages/channels_redis/core.py", line 78, in pop
conn = await aioredis.create_redis(**self.host)
File "/opt/homebrew/lib/python3.9/site-packages/aioredis/commands/__init__.py", line 168, in create_redis
conn = await create_connection(address, db=db,
File "/opt/homebrew/lib/python3.9/site-packages/aioredis/connection.py", line 83, in create_connection
address, options = parse_url(address)
File "/opt/homebrew/lib/python3.9/site-packages/aioredis/util.py", line 148, in parse_url
assert r.scheme in ('', 'redis', 'rediss', 'unix'), (
AssertionError: ('Unsupported URI scheme', 'http')
我遵循了https://channels.readthedocs.io/en/stable/deploying.html文档中的代码,我的asgi.py
样子如下:
import os
from django.core.asgi import get_asgi_application
from django.conf.urls import url
from django.urls import re_path
os.environ.setdefault("DJANGO_SETTINGS_MODULE", "mysite.settings")
django_asgi_app = get_asgi_application()
from channels.auth import AuthMiddlewareStack
from channels.routing import ProtocolTypeRouter, URLRouter
from users import consumers
websocket_urlpatterns = [
re_path(r'^ws/login$', consumers.UserLoginStatusConsumer.as_asgi()),
]
application = ProtocolTypeRouter({
# Django's ASGI application to handle traditional HTTP requests
'http': django_asgi_app,
# WebSocket handler
'websocket': AuthMiddlewareStack(
URLRouter(
websocket_urlpatterns
)
),
})
我也得到了我settings.py
的包括这个:
ASGI_APPLICATION = "mysite.asgi.application"
CHANNEL_LAYERS = {
"default": {
"BACKEND": "channels_redis.core.RedisChannelLayer",
"CONFIG": {
"hosts": [("127.0.0.1", 6379)],
},
},
}
最后我的consumer.py
文件看起来像这样:
class UserLoginStatusConsumer(AsyncWebsocketConsumer):
async def connect(self):
print("connected")
user = self.scope['user']
self.update_user_status(user, True)
print('Connection accepted: ' + user.email + ' online.')
async def disconnect(self):
user = self.scope['user']
self.update_user_status(user, False)
print('User disconnected.')
@database_sync_to_async
def update_user_status(self, user, status):
"""
Updates the user `status.
`status` can be one of the following status: 'online', 'offline' or 'away'
"""
print('user updated')
return User.objects.filter(pk=user.pk).update(login_status=status)
我错过了什么...?
先感谢您!非常感激。
解决方案
推荐阅读
- python - Python在没有__init__的类中调用def
- android - Flutter 滑动条动画
- javascript - Bootstrap TouchSpin,更改 initval
- forms - 如何在 Delphi 中限制给定变量的小数数量
- python-3.x - 如何在 python 中压缩 tar.gz 文件时包含目录?
- javascript - Vue-Auth:无法读取未定义的属性“beforeEach”
- r - R 脚本错误:“闭包”类型的对象不是子集。调用:聚合
- uiaccessibility - 如何使用 python 获取本机 mac 应用程序的 UI 元素?
- github - 从 GitHub 获取原始文件,无需等待 5 分钟缓存更新
- css - 在 REACT 应用程序上使用什么来进行造型并让它看起来很棒?Boostrap CDN、React-bootstrap、MDBoostrap 等?