File tree Expand file tree Collapse file tree 2 files changed +39
-2
lines changed Expand file tree Collapse file tree 2 files changed +39
-2
lines changed Original file line number Diff line number Diff line change 99
1010import os
1111
12+ from django .conf .urls import url
1213from django .core .asgi import get_asgi_application
1314
14- os .environ .setdefault (' DJANGO_SETTINGS_MODULE' , ' dj_idom.settings' )
15+ os .environ .setdefault (" DJANGO_SETTINGS_MODULE" , " dj_idom.settings" )
1516
16- application = get_asgi_application ()
17+ # Fetch ASGI application before importing dependencies that require ORM models.
18+ http_asgi_app = get_asgi_application ()
19+
20+ from channels .auth import AuthMiddlewareStack
21+ from channels .routing import ProtocolTypeRouter , URLRouter
22+ from channels .security .websocket import AllowedHostsOriginValidator
23+
24+ from .consumers import CommandConsumer
25+
26+ application = ProtocolTypeRouter (
27+ {
28+ # ASGI app has concurrency problems, see
29+ # See https://github.com/django/channels/issues/1587
30+ "http" : http_asgi_app ,
31+ "websocket" : AllowedHostsOriginValidator (
32+ AuthMiddlewareStack (URLRouter ([url ("" , CommandConsumer ().as_asgi ())]))
33+ ),
34+ }
35+ )
Original file line number Diff line number Diff line change 1+ """Anything used to construct a websocket endpoint"""
2+ from channels .generic .websocket import AsyncJsonWebsocketConsumer
3+
4+
5+ class CommandConsumer (AsyncJsonWebsocketConsumer ):
6+ """Websocket communication."""
7+
8+ # INITIAL CONNECTION
9+ async def connect (self ):
10+ """When the browser attempts to connect to the server."""
11+ # Accept the connection
12+ await self .accept ()
13+ pass
14+
15+ # RECEIVING COMMANDS
16+ async def receive_json (self , content , ** kwargs ):
17+ """When the browser attempts to send a message to the server."""
18+ pass
You can’t perform that action at this time.
0 commit comments