ASGI_APPLICATION ="django_template_v1.routing.application"
CHANNEL_LAYERS ={"default":{# This example apps uses the Redis channel layer implementation channels_redis"BACKEND":"channels_redis.core.RedisChannelLayer","CONFIG":{"hosts":["{}0".format(REDIS_URL)],},},}
2. 跟settings.py同级目录下,添加routing.py文件
from django.urls import path, re_path
from channels.auth import AuthMiddlewareStack
from channels.routing import ProtocolTypeRouter, URLRouter
from apps.message_manage.consumers import ChatConsumer, NotificationChatConsumer
# QueryAuthMiddlewareStack、AuthMiddlewareStack
application = ProtocolTypeRouter({"websocket": URLRouter([
re_path(r'^ws/chat/(?P<recipient>\w+)/$', ChatConsumer),# re_path(r'^ws/chatting/(?P<recipient>\w+)/$', NotificationChatConsumer),
re_path(r'^ws/chatting/(?P<recipient>\w+)/(?P<platform_key>\w+)/$', NotificationChatConsumer),]),})
3. 跟settings.py同级目录下,添加asgi.py文件
"""
ASGI config for django_template_v1 project.
It exposes the ASGI callable as a module-level variable named ``application``.
For more information on this file, see
https://docs.djangoproject.com/en/3.2/howto/deployment/asgi/
"""import os
import django
from channels.routing import get_default_application
os.environ.setdefault('DJANGO_SETTINGS_MODULE','django_template_v1.settings')
django.setup()
application = get_default_application()