def test_channel_name_router(): """ Tests the ChannelNameRouter """ # Test basic operation router = ChannelNameRouter( {"test": MagicMock(return_value=1), "other_test": MagicMock(return_value=2)} ) assert router({"channel": "test"}) == 1 assert router({"channel": "other_test"}) == 2 # Test an unmatched channel with pytest.raises(ValueError): router({"channel": "chat"}) # Test a scope with no channel with pytest.raises(ValueError): router({"type": "http"})
async def test_channel_name_router(): """ Tests the ChannelNameRouter """ # Test basic operation router = ChannelNameRouter( { "test": MockApplication(return_value=1), "other_test": MockApplication(return_value=2), } ) assert await router({"channel": "test"}, None, None) == 1 assert await router({"channel": "other_test"}, None, None) == 2 # Test an unmatched channel with pytest.raises(ValueError): await router({"channel": "chat"}, None, None) # Test a scope with no channel with pytest.raises(ValueError): await router({"type": "http"}, None, None)
# wireless_login/routing.py from channels.auth import AuthMiddlewareStack from channels.routing import ProtocolTypeRouter, URLRouter, ChannelNameRouter from garden import routing from garden import consumers from django.conf import settings BOX_CHANNEL = str("work_" + settings.BOX_SERIAL) application = ProtocolTypeRouter({ # (http->django views is added by default) 'websocket': AuthMiddlewareStack(URLRouter(routing.websocket_urlpatterns)), "channel": ChannelNameRouter({ BOX_CHANNEL: consumers.change_detect, }) }) ASGI_APPLICATION = "wireless_login.routing.application"
# from django.core.asgi import get_asgi_application import envdaq.routing import envnet.routing # os.environ.setdefault("DJANGO_SETTINGS_MODULE", "envdsys.settings") # os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'config.settings') # os.environ['DJANGO_SETTINGS_MODULE'] = 'config.settings' # django.setup() application = ProtocolTypeRouter({ "http": get_asgi_application(), "websocket": AuthMiddlewareStack( # [ URLRouter(envdaq.routing.websocket_urlpatterns + envnet.routing.websocket_urlpatterns) # ] ), "channel": ( # ChannelNameRouter( # envdaq.routing.channel_urlpatterns, # ) ChannelNameRouter({ **envnet.routing.channel_urlpatterns, **envdaq.routing.channel_urlpatterns, })) }) # application = get_default_application()
from channels.sessions import SessionMiddlewareStack from django.conf.urls import url, re_path from channels.routing import ProtocolTypeRouter, URLRouter, ChannelNameRouter from channels.auth import AuthMiddlewareStack from channels.security.websocket import AllowedHostsOriginValidator, OriginValidator from chat.consumers import ChatConsumer, TaskConsumer from render.consumers import RenderConsumer, ServerConsumer from sketchbook.consumers import SketchbookConsumer application = ProtocolTypeRouter({ # Websocket chat handler 'websocket': AllowedHostsOriginValidator( AuthMiddlewareStack( URLRouter( [ re_path(r"^messages/(?P<username>[\w.@+-]+)$", ChatConsumer(), name='chat'), re_path(r"^api/render/$", RenderConsumer(), name='render'), re_path(r"^server_info$", ServerConsumer(), name='server'), re_path(r"^ws/sketchbook/$", SketchbookConsumer().as_asgi(), name='sketchbook'), ] ) ), ), 'channel': ChannelNameRouter({ 'task': TaskConsumer }) })
""" Module with routing configuration used by Channels. Used to route requests with DIFFERENT protocols to be handled by different CONSUMERS (ASGI applications). ProtocolTypeRouter itself is the main ASGI application that wraps many consumers. This application is served to Daphne ASGI app server. """ from channels.auth import AuthMiddlewareStack from channels.http import AsgiHandler from channels.routing import ChannelNameRouter from channels.routing import ProtocolTypeRouter from channels.routing import URLRouter from channels.security.websocket import AllowedHostsOriginValidator from django_chat.apps.chat.consumers import SMSChannelConsumer from django_chat.apps.chat.urls import websocket_urlpatterns # main ASGI application for ASGI application servers application = ProtocolTypeRouter({ "http": AsgiHandler, "websocket": AllowedHostsOriginValidator( AuthMiddlewareStack(URLRouter(websocket_urlpatterns))), "channel": ChannelNameRouter({"sms_channel": SMSChannelConsumer}), })
from channels.routing import ChannelNameRouter, ProtocolTypeRouter, URLRouter from django.urls import path from .consumers import ClientConsumer, MainConsumer, WorkerConsumer from .protocol import CHANNEL_MAIN, CHANNEL_WORKER application = ProtocolTypeRouter( { # Client-facing consumers. 'websocket': URLRouter( [ # To change the prefix, you can import ClientConsumer in your custom # Channels routing definitions instead of using these defaults. path('ws/<slug:subscriber_id>', ClientConsumer.as_asgi()) ] ), # Background worker consumers. 'channel': ChannelNameRouter( { CHANNEL_MAIN: MainConsumer.as_asgi(), CHANNEL_WORKER: WorkerConsumer.as_asgi(), } ), } )
from channels.auth import AuthMiddlewareStack from channels.routing import ProtocolTypeRouter, ChannelNameRouter, URLRouter from django.conf.urls import url from search.websocketConsumer import WSConsumer from tweetCrawler.crawler import Crawler as tweet_crawler from googleCrawlerOfficial.crawler import Crawler as google_crawler from database.searcher import Searcher websocket_urlpatterns = [ url("", WSConsumer), ] application = ProtocolTypeRouter({ "channel": ChannelNameRouter({ "tweet_crawler": tweet_crawler, "google_crawler": google_crawler, "db_searcher": Searcher, }), 'websocket': AuthMiddlewareStack( URLRouter( websocket_urlpatterns ) ), })
from channels.auth import AuthMiddlewareStack from channels.routing import ProtocolTypeRouter, URLRouter, ChannelNameRouter import chat.routing from chat import consumers application = ProtocolTypeRouter({ 'websocket': AuthMiddlewareStack( URLRouter( chat.routing.websocket_urlpatterns ) ), 'chanel': ChannelNameRouter({ 'email-book': consumers.EmailBook, }), })
from channels.auth import AuthMiddlewareStack from channels.routing import ProtocolTypeRouter, ChannelNameRouter, URLRouter import tracker.routing application = ProtocolTypeRouter({ # (http->django views is added by default) 'websocket': AuthMiddlewareStack(URLRouter(tracker.routing.websocket_urlpatterns)), 'channel': ChannelNameRouter(tracker.routing.gnd_workers) })
from channels.routing import ProtocolTypeRouter, ChannelNameRouter, URLRouter from .consumers import LncConsumer, WorkerConsumer from django.conf.urls import url from channels.sessions import SessionMiddlewareStack application = ProtocolTypeRouter({ "websocket":SessionMiddlewareStack( URLRouter([ url("^lightning/ws$", LncConsumer), ])), 'channel': ChannelNameRouter({ 'lnws': WorkerConsumer, }), })
from channels.routing import ProtocolTypeRouter from channels.routing import URLRouter from channels.routing import ChannelNameRouter from channels.auth import AuthMiddlewareStack import main.routing application = ProtocolTypeRouter({ 'websocket': AuthMiddlewareStack(URLRouter(main.routing.websocket_urlpatterns)), 'channel': ChannelNameRouter(main.routing.channel_urlpatterns), })
from channels.auth import AuthMiddlewareStack from channels.routing import ProtocolTypeRouter, ChannelNameRouter, URLRouter import app.consumers import app.routing application = ProtocolTypeRouter({ 'websocket': AuthMiddlewareStack(URLRouter(app.routing.websocket_urlpatterns)), "channel": ChannelNameRouter({ "kafka-generate": app.consumers.CNCConsumer, }), })
from channels.auth import AuthMiddlewareStack from channels.routing import ProtocolTypeRouter, URLRouter, ChannelNameRouter from django.conf.urls import url from pool_ladder.consumers import MainConsumer, NotificationConsumer application = ProtocolTypeRouter({ # Empty for now (http->django views is added by default) 'websocket': AuthMiddlewareStack( URLRouter([ url(r'^pool-ladder/$', MainConsumer) ]) ), 'channel': ChannelNameRouter({ 'notifications': NotificationConsumer }), })
from django.conf.urls import url from channels.routing import ProtocolTypeRouter, URLRouter, ChannelNameRouter from channels.auth import AuthMiddlewareStack from channels.security.websocket import AllowedHostsOriginValidator from backend.consumers import AzterketaConsumer, BackgroundTaskConsumer application = ProtocolTypeRouter({ # Messages directed to a single channel will have a type `channel` 'channel': ChannelNameRouter({ # Messages directed to the `background-tasks` channel will be passed to our consumer 'background-tasks': BackgroundTaskConsumer, }), 'websocket': AllowedHostsOriginValidator( AuthMiddlewareStack( URLRouter( [ url(r'^ws/azterketak/(?P<token>[^/]+)/$', AzterketaConsumer), ] ) ) ) })
from channels.auth import AuthMiddlewareStack from channels.routing import ProtocolTypeRouter, URLRouter, ChannelNameRouter from django.urls import re_path from .consumers import GameConsumer, BackgroundConsumer application = ProtocolTypeRouter({ 'websocket': AuthMiddlewareStack( URLRouter([ re_path(r'api/dashboard/$', GameConsumer), ])), 'channel': ChannelNameRouter({'background-tasks': BackgroundConsumer}) })
""" ASGI config for project_solo_proj 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.auth import AuthMiddlewareStack from channels.routing import ProtocolTypeRouter, URLRouter, ChannelNameRouter from django.core.asgi import get_asgi_application from tasks import consumers import project_solo_app.routing os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'project_solo_proj.settings') application = ProtocolTypeRouter({ "http": get_asgi_application(), "websocket": AuthMiddlewareStack( URLRouter(project_solo_app.routing.websocket_urlpatterns)), "channel": ChannelNameRouter( {"background-tasks": consumers.BackgroundTaskConsumer.as_asgi()}), })
from channels.auth import AuthMiddlewareStack from channels.routing import ProtocolTypeRouter, URLRouter, ChannelNameRouter from django.conf.urls import url import team_match.public_resource import team_match.routing application = ProtocolTypeRouter({ # (http->django views is added by default) # 普通的HTTP请求不需要我们手动在这里添加,框架会自动加载过来 'websocket': AuthMiddlewareStack( URLRouter( team_match.routing.websocket_urlpatterns # [url('ws/chatroom/<str:room_name>/', chat.consumers.ChatConsumer),] ), ), "channel": ChannelNameRouter({ "service-detection": team_match.public_resource.BattleConsumer, }) })
from channels.auth import AuthMiddlewareStack from channels.routing import ProtocolTypeRouter, URLRouter, ChannelNameRouter from game.game_logic.game_manager import GameWorker import game.routing application = ProtocolTypeRouter({ # (http->django views is added by default) 'websocket': AuthMiddlewareStack(URLRouter(game.routing.websocket_urlpatterns)), 'channel': ChannelNameRouter({ 'game-manager': GameWorker, }), })
""" @software: PyCharm @file: routing.py @time: 2018/7/2 14:56 """ from channels.routing import ProtocolTypeRouter, URLRouter, ChannelNameRouter from channels.auth import AuthMiddlewareStack from channels.security.websocket import AllowedHostsOriginValidator import MyChannels.routing from channels.sessions import SessionMiddlewareStack import logging logger = logging.getLogger("log") # 根据请求协议来路由 application = ProtocolTypeRouter({ 'websocket': AllowedHostsOriginValidator( AuthMiddlewareStack(URLRouter(MyChannels.routing.ws_urlpatterns))), 'channel': ChannelNameRouter(MyChannels.routing.BackgroundTaskRouter) })
from channels.auth import AuthMiddlewareStack from channels.routing import ProtocolTypeRouter, URLRouter, ChannelNameRouter from django.urls import path #import control_system.routing from control_system.consumers import CommandConsumer from bluetooth_interface.consumers import BTConsumer application = ProtocolTypeRouter({ 'websocket': AuthMiddlewareStack( URLRouter([ path('devices/control/<int:device_id>', CommandConsumer), ])), 'channel': ChannelNameRouter({ 'bt-process': BTConsumer, }) })
from channels.auth import AuthMiddlewareStack from channels.routing import ProtocolTypeRouter, URLRouter, ChannelNameRouter from django.conf.urls import url import ws.consumers application = ProtocolTypeRouter({ 'websocket': AuthMiddlewareStack( URLRouter([ url(r'^ws/(?P<room_name>[^/]+)/$', ws.consumers.ChatConsumer), ]) ), "channel": ChannelNameRouter({ "ws": ws.consumers.ChatConsumer, "data_push": ws.consumers.DataServer }), })
from channels.routing import ProtocolTypeRouter, ChannelNameRouter from channels.auth import AuthMiddlewareStack from channels.routing import URLRouter import game.routing from game.consumers import RoomManagerConsumer from channels.auth import AuthMiddlewareStack application = ProtocolTypeRouter({ # (http->django views is added by default) 'channel':ChannelNameRouter({ 'room-manager':RoomManagerConsumer }), 'websocket': AuthMiddlewareStack( URLRouter( game.routing.websocket_urlpatterns ) ) })
from channels.auth import AuthMiddlewareStack from channels.routing import ChannelNameRouter, ProtocolTypeRouter, URLRouter from django.conf.urls import url from pdf_reactor import consumers from pdf_reactor.settings import PDF_REACTOR_CONFIG application = ProtocolTypeRouter({ 'websocket': AuthMiddlewareStack( URLRouter([ url( r"^status/(?P<job_id>\w+)/$", consumers.PdfReactorStatusConsumer, ), ])), "channel": ChannelNameRouter({ PDF_REACTOR_CONFIG['CHANNEL_NAME']: consumers.PdfReactorConsumer, }), })
# # Unless required by applicable law or agreed to in writing, software # distributed under the License is distributed on an "AS IS" BASIS, # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. # See the License for the specific language governing permissions and # limitations under the License. from channels.auth import AuthMiddlewareStack from channels.routing import ProtocolTypeRouter, URLRouter, ChannelNameRouter from channels.security.websocket import AllowedHostsOriginValidator from django.core.asgi import get_asgi_application from django.urls import re_path from console import routing as console_routing from stage import routing as stage_routing urlpatterns = stage_routing.websocket_urlpatterns + console_routing.urlpatterns http_urlpatterns = stage_routing.http_urlpatterns + [ re_path(r"", get_asgi_application()) ] application = ProtocolTypeRouter({ "http": AuthMiddlewareStack(URLRouter(http_urlpatterns)), "websocket": AllowedHostsOriginValidator(AuthMiddlewareStack(URLRouter(urlpatterns)), ), "channel": ChannelNameRouter(stage_routing.workers), })
from channels.routing import ProtocolTypeRouter, URLRouter, ChannelNameRouter from django.conf.urls import url from .apps.games.consumers import GamePlayingConsumer, GameWatchingConsumer """ These routes have fancy regex to pass their arguments through Basically, all the args are positive lookaheads (so they can be specified in any order, not that it matters), with the key inside and a named capturing group for a character subset afterwards. If you took Gabor AI you should know what that all means. """ application = ProtocolTypeRouter({ 'websocket': URLRouter([ url( r"^ws/play/(?=.*black=(?P<black>[\w\d-]+))(?=.*white=(?P<white>[\w\d-]+))(?=.*t=(?P<t>[\d\.]+))", GamePlayingConsumer), url(r"^ws/watch/(?=.*watching=(?P<watching>[\w\d]+))", GameWatchingConsumer), ]), 'channel': ChannelNameRouter({ # empty }) })
from channels.routing import ProtocolTypeRouter, ChannelNameRouter from telegram_bot.tasks import TelegramTask application = ProtocolTypeRouter({ # Empty for now (http->django views is added by default) "channel": ChannelNameRouter({ "telegram-task": TelegramTask, }), })
AuthMiddlewareStack( URLRouter([ # TournamentOverviewContainer url(r'^ws/(?P<tournament_slug>[-\w_]+)/action_logs/$', ActionLogEntryConsumer), url(r'^ws/(?P<tournament_slug>[-\w_]+)/ballot_results/$', BallotResultConsumer), url(r'^ws/(?P<tournament_slug>[-\w_]+)/ballot_statuses/$', BallotStatusConsumer), # CheckInStatusContainer url(r'^ws/(?P<tournament_slug>[-\w_]+)/checkins/$', CheckInEventConsumer), # Draw and Preformed Panel Edits url( r'^ws/(?P<tournament_slug>[-\w_]+)/round/(?P<round_seq>[-\w_]+)/debates/$', DebateEditConsumer), url( r'^ws/(?P<tournament_slug>[-\w_]+)/round/(?P<round_seq>[-\w_]+)/panels/$', PanelEditConsumer), ])), # Worker handlers (which don't need a URL/protocol) "channel": ChannelNameRouter({ # Name used in runworker cmd : SyncConsumer responsible "notifications": NotificationQueueConsumer, # Email sending "adjallocation": AdjudicatorAllocationWorkerConsumer, "venues": VenuesWorkerConsumer, }), })
from channels.routing import ChannelNameRouter, ProtocolTypeRouter from django.conf.urls import url from .consumers import Cpic application = ProtocolTypeRouter( {"channel": ChannelNameRouter({ "check-cpic": Cpic, })})
import asyncio from channels.routing import ProtocolTypeRouter, URLRouter, ChannelNameRouter from channels.auth import AuthMiddlewareStack from django.urls import re_path from channels.security.websocket import AllowedHostsOriginValidator from chat.consumers import ChatConsumer from chat.tasks import * websocket_urlpatterns = [ re_path(r'^chat$', ChatConsumer), ] application = ProtocolTypeRouter({ # WebSocket chat handler 'websocket': AllowedHostsOriginValidator( URLRouter(websocket_urlpatterns) ), 'channel': ChannelNameRouter({ 'matchmaking-task': MatchmakingTask, 'db-operations-task': DBOperationsTask, 'pn-task': PushNotificationsTask, 'conversation-manager-task': ConversationManagerTask }) })