我一直在尝试使用 Azure Web 应用程序和 azure redis 缓存为我的 django 应用程序设置生产环境。
在我的浏览器控制台中出现此错误:
(index):157 WebSocket connection to 'wss://<mysite>.azurewebsites.net/ws/chat/hey/' failed: Error during WebSocket handshake: Unexpected response code: 404
在我的settings.py 中,我尝试了几种不同的设置。我可以使用的最后一个
from django.core.cache import cache
cache.set("gg", "1234567")
print(cache.get("gg"))
我可以在 Azure 门户上的 Redis 控制台中查看事件。但我无法使用我怀疑需要的后端“channels_redis.core.RedisChannelLayer”。
CACHES = {
'default': {
'BACKEND': 'redis_cache.RedisCache',
'LOCATION' : '<my_azure_redis>.redis.cache.windows.net:6379',
'OPTIONS': {
'PASSWORD': '<mypassword>',
'DB': '1'
},
}
}
CHANNEL_LAYERS = {
'default': {
'BACKEND': 'channels_redis.core.RedisChannelLayer',
'CONFIG': {
'HOST': '<my_azure_redis>.redis.cache.windows.net',
'PORT': '6379',
'PASSWORD': '<mypassword>',
'SSL' : False,
}
}
}
CACHES = {
'default': {
'BACKEND': 'redis_cache.RedisCache',
'LOCATION': '%s:%s' % ("<my_azure_redis>.redis.cache.windows.net", 6379),
'OPTIONS': {
'PASSWORD': "<mypassword>",
'DB': 0,
}
}
}
ASGI_APPLICATION = "django_azure_demo.routing.application"
JavaScript:
const socket = new WebSocket(
'wss://'
+ window.location.host
+ '/ws/chat/'
+ 'hey'
+ '/'
);
路由.py:
websocket_urlpatterns = [
re_path(r'ws/chat/hey/', consumer.ChatConsumer),
]
asgi.py
import os
from django.core.asgi import get_asgi_application
os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'django_azure_demo.settings')
application = get_asgi_application()
路由.py
application = ProtocolTypeRouter({
'websocket': AllowedHostsOriginValidator(
AuthMiddlewareStack(
URLRouter(
food.routing.websocket_urlpatterns
)
)
)
})
这一切都在开发中。
我跟随https://learn.microsoft.com/en-us/azure/azure-cache-for-redis/cache-python-get-started开始使用redis。我能够从我的 Redis 存储中设置和检索变量。
我还在 Azure 缓存中为 Redis 打开了 6379 端口,以允许非 SSL。
我现在一片空白,不知道下一步该去哪里。感谢您的任何意见!
最佳答案
如果有人仍然想知道你是如何做到的
将您的凭据存储在变量中
myHostname = ""
myPassword = ""
并使用此 channel 设置
CHANNEL_LAYERS = {
'default': {
'BACKEND': 'channels_redis.core.RedisChannelLayer',
'CONFIG': {
'hosts': [('rediss://:{password}@{hostname}:{port}'.format(
password=myPassword,
hostname=myHostname,
port=6380
))],
},
},
}
关于python - 具有 azure Web 应用程序和存储的 Django channel ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/61992783/