File tree Expand file tree Collapse file tree 5 files changed +53
-11
lines changed Expand file tree Collapse file tree 5 files changed +53
-11
lines changed Original file line number Diff line number Diff line change 1717# Fetch ASGI application before importing dependencies that require ORM models.
1818http_asgi_app = get_asgi_application ()
1919
20- from channels .auth import AuthMiddlewareStack
2120from channels .routing import ProtocolTypeRouter , URLRouter
22- from channels .security .websocket import AllowedHostsOriginValidator
2321
2422from .consumers import CommandConsumer
2523
2624application = ProtocolTypeRouter (
2725 {
28- # ASGI app has concurrency problems, see
29- # See https://github.com/django/channels/issues/1587
3026 "http" : http_asgi_app ,
31- "websocket" : AllowedHostsOriginValidator (
32- AuthMiddlewareStack (URLRouter ([url ("" , CommandConsumer ().as_asgi ())]))
33- ),
27+ "websocket" : URLRouter ([url ("" , CommandConsumer ().as_asgi ())]),
3428 }
3529)
Original file line number Diff line number Diff line change 3737 "django.contrib.sessions" ,
3838 "django.contrib.messages" ,
3939 "django.contrib.staticfiles" ,
40+ "channels" , # Websocket library
4041]
4142
4243MIDDLEWARE = [
123124# https://docs.djangoproject.com/en/3.2/ref/settings/#default-auto-field
124125
125126DEFAULT_AUTO_FIELD = "django.db.models.BigAutoField"
127+
128+ # Static Files (CSS, JavaScript, Images)
129+ STATICFILES_DIRS = [
130+ os .path .join (BASE_DIR , "dj_idom" , "static" ),
131+ ]
Original file line number Diff line number Diff line change 1+ // Set up a websocket at the base endpoint
2+ let LOCATION = window . location ;
3+ let WS_PROTOCOL = "" ;
4+ if ( LOCATION . protocol == "https:" ) {
5+ WS_PROTOCOL = "wss://" ;
6+ } else {
7+ WS_PROTOCOL = "ws://" ;
8+ }
9+ let WS_ENDPOINT_URL = WS_PROTOCOL + LOCATION . host ;
10+ let COMMAND_SOCKET = new WebSocket ( WS_ENDPOINT_URL ) ;
11+
12+ // Receivable commands
13+ COMMAND_SOCKET . onmessage = function ( response ) {
14+ // Websocket message received, parse for JSON
15+ console . info ( response ) ;
16+ json_response = JSON . parse ( response . data ) ;
17+
18+ // Check for valid commands
19+ console . info ( "Websocket has recieved a message" , json_response ) ;
20+ } ;
21+
22+ // Websocket open event
23+ COMMAND_SOCKET . onopen = function ( ) {
24+ console . info ( "Websocket has opened." ) ;
25+ } ;
26+
27+ // Websocket close event
28+ COMMAND_SOCKET . onclose = function ( ) {
29+ console . info ( "Websocket has closed." ) ;
30+ } ;
31+
32+ // Websocket error event
33+ COMMAND_SOCKET . onerror = function ( error ) {
34+ console . error (
35+ "Websocket encountered a crtical error: " ,
36+ error . message ,
37+ "Closing socket..."
38+ ) ;
39+ COMMAND_SOCKET . close ( ) ;
40+ } ;
Original file line number Diff line number Diff line change 1+ {% load static %}
12<!DOCTYPE html>
23< html lang ="en ">
34
45< head >
56 < meta charset ="UTF-8 ">
67 < meta http-equiv ="X-UA-Compatible " content ="IE=edge ">
78 < meta name ="viewport " content ="width=device-width, initial-scale=1.0 ">
9+ < script src ="{% static 'scripts.js' %} " crossorigin ="anonymous "> </ script >
810 < title > IDOM</ title >
911</ head >
1012
Original file line number Diff line number Diff line change 1- django < 4.0.0
2- daphne < 4.0.0
3- channels < 4.0.0
4- idom < 1.0.0
1+ django < 4.0.0 # Django Library
2+ daphne < 4.0.0 # Production ASGI webserver
3+ channels < 4.0.0 # Django websocket features
4+ idom < 1.0.0 # Python React
You can’t perform that action at this time.
0 commit comments