Ejemplo n.º 1
0
 def handle(self, *args, **options):
     # Get the backend to use
     self.verbosity = options.get("verbosity", 1)
     self.logger = setup_logger('django.channels', self.verbosity)
     self.channel_layer = channel_layers[options.get(
         "layer", DEFAULT_CHANNEL_LAYER)]
     # Check that handler isn't inmemory
     if self.channel_layer.local_only():
         raise CommandError(
             "You cannot span multiple processes with the in-memory layer. "
             + "Change your settings to use a cross-process channel layer.")
     # Check a handler is registered for http reqs
     # Serve static files if Django in debug mode
     if settings.DEBUG:
         self.channel_layer.router.check_default(
             http_consumer=StaticFilesConsumer())
     else:
         self.channel_layer.router.check_default()
     # Launch a worker
     self.logger.info("Running worker against channel layer %s",
                      self.channel_layer)
     # Optionally provide an output callback
     callback = None
     if self.verbosity > 1:
         callback = self.consumer_called
     # Run the worker
     try:
         Worker(
             channel_layer=self.channel_layer,
             callback=callback,
             only_channels=options.get("only_channels", None),
             exclude_channels=options.get("exclude_channels", None),
         ).run()
     except KeyboardInterrupt:
         pass
 def get_consumer(self, *args, **options):
     """
     Returns the static files serving handler wrapping the default handler,
     if static files should be served. Otherwise just returns the default
     handler.
     """
     use_static_handler = options.get('use_static_handler', True)
     insecure_serving = options.get('insecure_serving', False)
     if use_static_handler and (settings.DEBUG or insecure_serving):
         return StaticFilesConsumer()
     else:
         return ViewConsumer()
Ejemplo n.º 3
0
 def handle(self, *args, **options):
     # Get the backend to use
     self.verbosity = options.get("verbosity", 1)
     self.logger = setup_logger('django.channels', self.verbosity)
     self.channel_layer = channel_layers[options.get(
         "layer", DEFAULT_CHANNEL_LAYER)]
     self.n_threads = options.get('threads', 1)
     # Check that handler isn't inmemory
     if self.channel_layer.local_only():
         raise CommandError(
             "You cannot span multiple processes with the in-memory layer. "
             + "Change your settings to use a cross-process channel layer.")
     # Check a handler is registered for http reqs
     # Serve static files if Django in debug mode
     if settings.DEBUG:
         self.channel_layer.router.check_default(
             http_consumer=StaticFilesConsumer())
     else:
         self.channel_layer.router.check_default()
     # Optionally provide an output callback
     callback = None
     if self.verbosity > 1:
         callback = self.consumer_called
     self.callback = callback
     self.options = options
     # Choose an appropriate worker.
     worker_kwargs = {}
     if self.n_threads == 1:
         self.logger.info("Using single-threaded worker.")
         worker_cls = Worker
     else:
         self.logger.info(
             "Using multi-threaded worker, {} thread(s).".format(
                 self.n_threads))
         worker_cls = WorkerGroup
         worker_kwargs['n_threads'] = self.n_threads
     # Run the worker
     self.logger.info("Running worker against channel layer %s",
                      self.channel_layer)
     try:
         worker = worker_cls(
             channel_layer=self.channel_layer,
             callback=self.callback,
             only_channels=self.options.get("only_channels", None),
             exclude_channels=self.options.get("exclude_channels", None),
             **worker_kwargs)
         worker_process_ready.send(sender=worker)
         worker.ready()
         worker.run()
     except KeyboardInterrupt:
         pass
Ejemplo n.º 4
0
from channels.staticfiles import StaticFilesConsumer
from channels import route
from . import consumers

channel_routing = [
    # This makes Django serve static files from settings.STATIC_URL, similar
    # to django.views.static.serve. This isn't ideal (not exactly production
    # quality) but it works for a minimal example.
    route('http.request', StaticFilesConsumer()),

    # Wire up websocket channels to our consumers:
    route('websocket.connect',
          consumers.connect_to_nifty_channel,
          path=r'^/nifty-channel/'),
    route(
        'websocket.disconnect',
        consumers.disconnect_from_nifty_channel,
        path=r'^/nifty-channel/',
    ),
    route('websocket.connect',
          consumers.connect_to_leaderboard_channel,
          path=r'^/leaderboard-channel/'),
    route(
        'websocket.disconnect',
        consumers.disconnect_from_leaderboard_channel,
        path=r'^/leaderboard-channel/',
    ),
    route('websocket.connect',
          consumers.connect_to_ticker_channel,
          path=r'^/ticker-channel/'),
    route(
Ejemplo n.º 5
0
from channels import route
from channels.staticfiles import StaticFilesConsumer

from .consumers import ws_connect, ws_receive, ws_disconnect, chat_join, chat_leave, chat_send

http_routing = [
    route("http.request", StaticFilesConsumer()),
]

# There's no path matching on these routes; we just rely on the matching
# from the top-level routing. We _could_ path match here if we wanted.
websocket_routing = [
    # Called when WebSockets connect
    route("websocket.connect", ws_connect),

    # Called when WebSockets get sent a data frame
    route("websocket.receive", ws_receive),

    # Called when WebSockets disconnect
    route("websocket.disconnect", ws_disconnect),
]

# You can have as many lists here as you like, and choose any name.
# Just refer to the individual names in the include() function.
custom_routing = [
    # Handling different chat commands (websocket.receive is decoded and put
    # onto this channel) - routed on the "command" attribute of the decoded
    # message.
    route("chat.receive", chat_join, command="^join$"),
    route("chat.receive", chat_leave, command="^leave$"),
    route("chat.receive", chat_send, command="^send$"),
Ejemplo n.º 6
0
from channels.staticfiles import StaticFilesConsumer
from chat import consumers

channel_routing = {
    # This makes Django serve static files from settings.STATIC_URL, similar
    # to django.views.static.serve. This isn't ideal (not exactly production
    # quality) but it works for a minimal example.
    'http.request': StaticFilesConsumer(),

    # Wire up websocket channels to our consumers:
    'websocket.connect': consumers.ws_connect,
    'websocket.receive': consumers.ws_receive,
    'websocket.disconnect': consumers.ws_disconnect,
}
Ejemplo n.º 7
0
from channels.routing import route_class, include
from .consumers import *

from channels.staticfiles import StaticFilesConsumer

http_routing = {'http.request': StaticFilesConsumer()}

channel_routing = [
    route_class(BookingConsumer, path=r"^/booking."),
    include(http_routing)
]
Ejemplo n.º 8
0
# In routing.py
from django.conf import settings

from channels.routing import route, include
from channels.staticfiles import StaticFilesConsumer
from channels.handler import ViewConsumer

from seiketsu.graphql.routing import channel_routing as graphql_routing

channel_routing = [
    include(graphql_routing, path="^/graphql"),
    route("http.request", StaticFilesConsumer()) if settings.DEBUG else route("http.request", ViewConsumer()),
]