from django.conf.urls import url from channels.routing import ProtocolTypeRouter, URLRouter from channels.auth import AuthMiddlewareStack from channels.security.websocket import AllowedHostsOriginValidator, OriginValidator from foxbinary.consumers import BinaryConsumer application = ProtocolTypeRouter({ # WebSocket handler "websocket": AllowedHostsOriginValidator( AuthMiddlewareStack(URLRouter([url(r"^ml/", BinaryConsumer)]))) })
from django.urls import path from channels.auth import AuthMiddlewareStack from channels.routing import ProtocolTypeRouter, URLRouter from channels.security.websocket import AllowedHostsOriginValidator from zihu_clone.messager.consumers import MessagesConsumer from zihu_clone.notifications.consumers import NotificationsConsumer application = ProtocolTypeRouter({ 'websocket': AllowedHostsOriginValidator( AuthMiddlewareStack( URLRouter([ path('ws/notifications/', NotificationsConsumer), path('ws/<str:username>/', MessagesConsumer), ]))) })
""" ASGI config for Game 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.1/howto/deployment/asgi/ """ import os from channels.routing import ProtocolTypeRouter, URLRouter from channels.auth import AuthMiddlewareStack from channels.security.websocket import AllowedHostsOriginValidator from django.core.asgi import get_asgi_application os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'mysite.settings') application = ProtocolTypeRouter({ 'WebSocket': AllowedHostsOriginValidator(AuthMiddlewareStack(URLRouter([]))) })
from channels.auth import AuthMiddlewareStack from channels.routing import ProtocolTypeRouter, URLRouter from channels.security.websocket import AllowedHostsOriginValidator from django.urls import path import tracker.routing application = ProtocolTypeRouter({ 'websocket': AllowedHostsOriginValidator( AuthMiddlewareStack( URLRouter([ path('tracker/', URLRouter(tracker.routing.websocket_urlpatterns)) ]))), })
from channels.auth import AuthMiddlewareStack from channels.routing import ProtocolTypeRouter, URLRouter from channels.security.websocket import AllowedHostsOriginValidator from django.urls import path, re_path from chat.consumers import ChatConsumer # from public_chat.consumers import PublicChatConsumer # from notification.consumers import NotificationConsumer application = ProtocolTypeRouter({ 'websocket': AllowedHostsOriginValidator( AuthMiddlewareStack( URLRouter([ #path('', NotificationConsumer), path('chat/<room_id>/', ChatConsumer), #path('public_chat/<room_id>/', PublicChatConsumer), ]))), })
from channels.routing import ProtocolTypeRouter, URLRouter from django.urls import path from channels.auth import AuthMiddlewareStack from channels.security.websocket import AllowedHostsOriginValidator, AllowedHostsOriginValidator # AllowedHostOriginValidator - Only ALLOWED_HOSTS in settings.py can connect # OriginValidator - We can have specific origin for just channels using this. from notif.consumers import NoseyConsumer, LikeNotificationConsumer application = ProtocolTypeRouter({ "websocket": AllowedHostsOriginValidator( AuthMiddlewareStack( URLRouter( # Inside below list - similar to standard urls.py [ # Consumer is similar to views in Django. path("notifications/", NoseyConsumer), path("like_notification/", LikeNotificationConsumer), ]), )) })
from channels.routing import ProtocolTypeRouter, URLRouter from channels.security.websocket import AllowedHostsOriginValidator from django.conf.urls import url from campaigns.notifications import consumers as notifications_consumers from common.authentication import AuthorizationProcessorBasedMiddlewareStack application = ProtocolTypeRouter({ "websocket": AllowedHostsOriginValidator( AuthorizationProcessorBasedMiddlewareStack( URLRouter([ url(r"^nyt/$", notifications_consumers.NotificationsConsumer), ]))), })
from channels.routing import ProtocolTypeRouter, URLRouter from django.conf.urls import url from channels.auth import AuthMiddlewareStack from channels.security.websocket import AllowedHostsOriginValidator, OriginValidator from chatapp.consumers import ChatConsumer application = ProtocolTypeRouter({ # Empty for now (http->django views is added by default) 'websocket': AllowedHostsOriginValidator( AuthMiddlewareStack(URLRouter([ url('chat/chat_room/', ChatConsumer), ]))) })
from django.urls import re_path from channels.routing import ProtocolTypeRouter, URLRouter, ChannelNameRouter from channels.auth import AuthMiddlewareStack from channels.security.websocket import AllowedHostsOriginValidator, OriginValidator from django_notifications_app.consumers import NotificationConsumer # Importing notification Consumer from consumers.py application = ProtocolTypeRouter({ # Websocket chat handler 'websocket': AllowedHostsOriginValidator( # Only allow socket connections from the Allowed hosts in the settings.py file AuthMiddlewareStack( # Session Authentication, required to use if we want to access the user details in the consumer URLRouter([ re_path( r'notifications/(?P<stream>\w+)/$', NotificationConsumer ), # Url path for connecting to the websocket to send notifications. ])), ), })
"""Routing for websocket""" from channels.auth import AuthMiddlewareStack from channels.routing import ProtocolTypeRouter, URLRouter from channels.security.websocket import AllowedHostsOriginValidator from django.urls import path from notifications.consumers import NotificationConsumer from private_chat.consumers import PrivateChatConsumer from public_chat.consumers import PublicChatConsumer application = ProtocolTypeRouter({ "websocket": AllowedHostsOriginValidator( AuthMiddlewareStack( URLRouter([ path("public-chat/<int:room_id>/", PublicChatConsumer.as_asgi()), path("private-chat/<int:room_id>/", PrivateChatConsumer.as_asgi()), path("", NotificationConsumer.as_asgi()), ]))) })
from django.conf.urls import url from channels.routing import ProtocolTypeRouter, URLRouter from channels.auth import AuthMiddlewareStack from channels.security.websocket import AllowedHostsOriginValidator, OriginValidator from chat.consumers import ChatConsumer # Соединение адреса с функцией обработки вебсокета application = ProtocolTypeRouter({ # Empty for now (http->django views is added by default) 'websocket': AllowedHostsOriginValidator( AuthMiddlewareStack( URLRouter([ url(r'^chat/(?P<username>\w+)/$', ChatConsumer), ]))) })
For more information on this file, see https://docs.djangoproject.com/en/3.0/howto/deployment/asgi/ """ import os from django.conf.urls import url from django.core.asgi import get_asgi_application # Fetch Django ASGI application early to ensure AppRegistry is populated # before importing consumers and AuthMiddlewareStack that may import ORM # models. os.environ.setdefault("DJANGO_SETTINGS_MODULE", "conreq.settings") django_asgi_app = get_asgi_application() from channels.auth import AuthMiddlewareStack from channels.routing import ProtocolTypeRouter, URLRouter from channels.security.websocket import AllowedHostsOriginValidator from conreq.utils.server_websockets import CommandConsumer application = ProtocolTypeRouter( { "http": django_asgi_app, "websocket": AllowedHostsOriginValidator( AuthMiddlewareStack(URLRouter([url("", CommandConsumer().as_asgi())])) ), } )
from django.conf.urls import url from channels.routing import ProtocolTypeRouter, URLRouter from channels.auth import AuthMiddlewareStack from channels.security.websocket import AllowedHostsOriginValidator, AllowedHostsOriginValidator from MentalMathWebsite.consumers import FindGameConsumer, GameConsumer, SoloGameConsumer from django.urls import path application = ProtocolTypeRouter({ 'websocket': AllowedHostsOriginValidator( AuthMiddlewareStack( URLRouter([ path("MentalMathWebsite/findGame", FindGameConsumer), path("MentalMathWebsite/<username1>/<username2>", GameConsumer), path("MentalMathWebsite/<username>", SoloGameConsumer) ]))) })
from django.urls import path from channels.auth import AuthMiddlewareStack from channels.routing import ProtocolTypeRouter, URLRouter from channels.security.websocket import AllowedHostsOriginValidator from real_estate_api.messager.consumers import MessagerConsumer application = ProtocolTypeRouter({ "websocket": AllowedHostsOriginValidator( AuthMiddlewareStack(URLRouter([ path('', MessagerConsumer), ])), ), })
import os from django.core.asgi import get_asgi_application os.environ.setdefault("DJANGO_SETTINGS_MODULE", "settings") django_asgi_app = get_asgi_application() from channels.auth import AuthMiddlewareStack from channels.routing import ProtocolTypeRouter, URLRouter from channels.security.websocket import AllowedHostsOriginValidator import chat.routing application = ProtocolTypeRouter({ "http": django_asgi_app, "websocket": AllowedHostsOriginValidator( AuthMiddlewareStack( URLRouter( chat.routing.websocket_urlpatterns ) ) ), })
from django.urls import path from channels.routing import ProtocolTypeRouter, URLRouter from channels.security.websocket import AllowedHostsOriginValidator from channels.auth import AuthMiddlewareStack from channels.sessions import SessionMiddlewareStack from wga.assets_user import consumers application = ProtocolTypeRouter({ 'websocket': AllowedHostsOriginValidator( AuthMiddlewareStack( SessionMiddlewareStack( URLRouter([ path('ws/wganalogy_app/user/<str:url_key>/', consumers.GameConsumer), path('ws/wganalogy_app/nav', consumers.NavBarConsumer) ])))), })
from channels.routing import ProtocolTypeRouter, URLRouter from django.urls import path from channels.auth import AuthMiddlewareStack from channels.security.websocket import AllowedHostsOriginValidator from gce_app.consumers import MainConsumer application = ProtocolTypeRouter({ # Empty for now (http->django views is added by default) 'websocket': AllowedHostsOriginValidator( AuthMiddlewareStack(URLRouter([path("", MainConsumer)]))), })
from channels.auth import AuthMiddlewareStack from channels.routing import ProtocolTypeRouter, URLRouter from channels.security.websocket import AllowedHostsOriginValidator from django.urls import path from zanhu.messager.consumers import MessagesConsumer from zanhu.notifications.consumers import NotificationsConsumer application = ProtocolTypeRouter({ # 'http': # 普通的HTTP请求不需要我们手动在这里添加,框架会自动加载 'websocket': # 使用AllowedHostsOriginValidator,允许的访问的源站与settings.py文件中的ALLOWED_HOSTS相同 AllowedHostsOriginValidator( # 认证中间件站(兼容Django认证系统):AuthMiddlewareStack用于WebSocket认证,集成了CookieMiddleware, SessionMiddleware, AuthMiddleware AuthMiddlewareStack( # URL路由 URLRouter([ # URL路由匹配 path('ws/notifications/', NotificationsConsumer), path('ws/<str:username>/', MessagesConsumer), # TODO: 这里路由的顺序 # Django和Consumers的路由是兼容的,也就是说,我们需要处理考虑这里路由请求的顺序 # 如果把ws/<str:username>/ 放在上面,notifications就是一个字符串,就直接使用username的路由解析了 # 这个情况,github都会存在的 ]))) })
from django.conf.urls import url from channels.routing import ProtocolTypeRouter, URLRouter from channels.auth import AuthMiddlewareStack from channels.security.websocket import AllowedHostsOriginValidator, OriginValidator from sinapp.consumers import SINConsumer application = ProtocolTypeRouter({ 'websocket': AllowedHostsOriginValidator( AuthMiddlewareStack(URLRouter([ url("SIN", SINConsumer.as_asgi()), ]))) })
from channels.security.websocket import AllowedHostsOriginValidator from channels.auth import AuthMiddlewareStack from channels.routing import ProtocolTypeRouter, URLRouter from model_sockets import routing as model_sockets_router from helper import routing as helper_router def flatten(l): flat = [] for sublist in l: for item in sublist: flat.append(item) return flat application = ProtocolTypeRouter({ # (http->django views is added by default) 'websocket': AllowedHostsOriginValidator( AuthMiddlewareStack( URLRouter( flatten([ model_sockets_router.websocket_urlpatterns, helper_router.websocket_urlpatterns ]) ) ), ) })
from django.conf.urls import url from channels.auth import AuthMiddlewareStack from channels.routing import ProtocolTypeRouter, URLRouter from channels.security.websocket import AllowedHostsOriginValidator from bootcamp.messager.consumers import MessagerConsumer from bootcamp.notifications.consumers import NotificationsConsumer # from bootcamp.notifications.routing import notifications_urlpatterns # from bootcamp.messager.routing import messager_urlpatterns application = ProtocolTypeRouter({ "websocket": AllowedHostsOriginValidator( AuthMiddlewareStack( URLRouter([ url(r"^notifications/$", NotificationsConsumer), url(r"^(?P<username>[^/]+)/$", MessagerConsumer), ]))) })
from channels.auth import AuthMiddlewareStack from channels.routing import ProtocolTypeRouter, URLRouter from channels.security.websocket import AllowedHostsOriginValidator from django.conf.urls import url from order.order_consumers import OrderConsumer application = ProtocolTypeRouter({ 'websocket': AllowedHostsOriginValidator( URLRouter([url(r'^ws/order_socket/$', OrderConsumer)])) })
from django.conf.urls import url from channels.routing import ProtocolTypeRouter, URLRouter from channels.auth import AuthMiddlewareStack from channels.security.websocket import AllowedHostsOriginValidator, OriginValidator from chat.consumers import ChatConsumer application = ProtocolTypeRouter({ # Empty for now (http->django views is added by default) 'websocket': AllowedHostsOriginValidator( AuthMiddlewareStack( URLRouter( [ url(r"^messages/(?P<username>[\w.@+-]+)/$", ChatConsumer) ]) ) ) })
from django.conf.urls import url from channels.routing import ProtocolTypeRouter, URLRouter from channels.auth import AuthMiddlewareStack from channels.security.websocket import AllowedHostsOriginValidator, OriginValidator import chat.routing application = ProtocolTypeRouter({ 'websocket': AllowedHostsOriginValidator(URLRouter(chat.routing.websocket_urlpatterns)), })
from channels.auth import AuthMiddlewareStack from channels.routing import ProtocolTypeRouter, URLRouter from channels.security.websocket import AllowedHostsOriginValidator from django.urls import path, re_path from public_chat.consumers import PublicChatConsumer from chat.consumers import ChatConsumer from notification.consumers import NotificationConsumer application = ProtocolTypeRouter({ 'websocket': AllowedHostsOriginValidator( AuthMiddlewareStack( URLRouter([ path('', NotificationConsumer), path("chat/<room_id>/", ChatConsumer), path("public_chat/<room_id>/", PublicChatConsumer), ]) ) ), })
from django.conf.urls import re_path from channels.routing import ProtocolTypeRouter, URLRouter from channels.auth import AuthMiddlewareStack from channels.security.websocket import AllowedHostsOriginValidator, OriginValidator from chat.consumers import ChatConsumer application = ProtocolTypeRouter({ # Websocket chat handler 'websocket': AllowedHostsOriginValidator( AuthMiddlewareStack( URLRouter([ #url(r"chat/", ChatConsumer, name='chat') re_path(r"messages/(?P<username>[\w.@+-]+)", ChatConsumer, name='chat') ])), ) })
from django.conf.urls import url from channels.routing import ProtocolTypeRouter, URLRouter from channels.auth import AuthMiddlewareStack from channels.security.websocket import AllowedHostsOriginValidator from chat.consumers import ChatConsumer application = ProtocolTypeRouter({ # Websocket chat handler 'websocket': AllowedHostsOriginValidator( AuthMiddlewareStack( URLRouter([url(r"chat/", ChatConsumer, name='chat')])), ) })
from zanhu.messager.consumer import MessagesConsumer from zanhu.notifications.consumers import NotificationsConsumer # self.scope['type']获取协议类型 # self.scope['url_route']['kwargs'] ['username']获取url中关键字参数, # 相当于views中获取路由参数的 return reverse("xxx路由",kwargs={"传递字段名":self.kwargs['路由参数']}) # channels routing是scope级别的,一个连接只能由一个consumer接收和处理 application = ProtocolTypeRouter({ # "http":views # 普通的HTTP请求不需要我们手动在这里添加,框架会自动加载 # AllowedHosts 读取我们在.env中定义的DJANGO_ALLOWED_HOSTS,读取对应的IP和域名 'websocket': AllowedHostsOriginValidator( AuthMiddlewareStack( URLRouter([ # 和http协议区分在前面添加 ws/ path('ws/notifications/', NotificationsConsumer), path('ws/<str:username>/', MessagesConsumer), ]) ) ) }) """ OriginValidator或AllowedHostsOriginValidator可以防止通过WebSocket进行CSRF攻击 from channels.security.websocket import AllowedHostsOriginValidator 可以读取我们settings中定义的DJANGO_ALLOWED_HOSTS(.env中定义的DJANGO_ALLOWED_HOSTS),读取对应的IP和域名 from channels.security.websocket import OriginValidator OriginValidator需要手动添加允许访问的源站,如:
from django.conf.urls import url from channels.routing import ProtocolTypeRouter, URLRouter, ChannelNameRouter from channels.auth import AuthMiddlewareStack from channels.security.websocket import AllowedHostsOriginValidator, OriginValidator from chat.consumers import ChatConsumer application = ProtocolTypeRouter({ # Websocket chat handler 'websocket': AllowedHostsOriginValidator( AuthMiddlewareStack( URLRouter([ #url(r"chat/", ChatConsumer, name='chat'), url(r'^ws/chat/(?P<room_name>[^/]+)/$', ChatConsumer), ])), ), 'channel': ChannelNameRouter({}) })
from django.urls import path from django.conf.urls import url from channels.auth import AuthMiddlewareStack from channels.routing import ProtocolTypeRouter, URLRouter from channels.security.websocket import AllowedHostsOriginValidator, OriginValidator from kurye.consumers import KuryeConsumer, RestoranConsumer application = ProtocolTypeRouter({ 'websocket': AllowedHostsOriginValidator( AuthMiddlewareStack( URLRouter([ url(r'^kurye/(?P<ticket>\w+)/$', KuryeConsumer), url(r'^restoran/$', RestoranConsumer), ]))) })