example-in-django-waiting-r.../myproject/asgi.py
Andros Fenollosa b97d9383a5 Add files
2024-11-05 11:46:00 +01:00

32 lines
929 B
Python

"""
ASGI config for myproject 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/5.1/howto/deployment/asgi/
"""
import os
os.environ.setdefault("DJANGO_SETTINGS_MODULE", "settings")
import django
django.setup()
from django.core.asgi import get_asgi_application
from channels.security.websocket import AllowedHostsOriginValidator
from channels.routing import ProtocolTypeRouter, URLRouter
from django.urls import re_path
from waiting_room.consumers import MyConsumer
application = ProtocolTypeRouter(
{
# Django's ASGI application to handle traditional HTTP requests
"http": get_asgi_application(),
# WebSocket handler
"websocket": AllowedHostsOriginValidator(
URLRouter([re_path(r"^ws/(?P<room_name>[a-zA-Z0-9_]+)/$", MyConsumer.as_asgi())])
),
}
)