首页 > 解决方案 > 带有 azure web 应用程序和存储的 Django 频道

问题描述

我一直在尝试使用 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://docs.microsoft.com/en-us/azure/azure-cache-for-redis/cache-python-get-started开始使用 redis。而且我能够从我的 redis 存储中设置和检索一个变量。

我还在 azure cache 中为 redis 打开了 6379 端口,以允许非 SSL。

我在这里一片空白,不知道下一步该去哪里。感谢您的任何意见!

标签: pythondjangoazurewebsocketredis

解决方案


推荐阅读