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 17
17
# Fetch ASGI application before importing dependencies that require ORM models.
18
18
http_asgi_app = get_asgi_application ()
19
19
20
- from channels .auth import AuthMiddlewareStack
21
20
from channels .routing import ProtocolTypeRouter , URLRouter
22
- from channels .security .websocket import AllowedHostsOriginValidator
23
21
24
22
from .consumers import CommandConsumer
25
23
26
24
application = ProtocolTypeRouter (
27
25
{
28
- # ASGI app has concurrency problems, see
29
- # See https://github.com/django/channels/issues/1587
30
26
"http" : http_asgi_app ,
31
- "websocket" : AllowedHostsOriginValidator (
32
- AuthMiddlewareStack (URLRouter ([url ("" , CommandConsumer ().as_asgi ())]))
33
- ),
27
+ "websocket" : URLRouter ([url ("" , CommandConsumer ().as_asgi ())]),
34
28
}
35
29
)
Original file line number Diff line number Diff line change 37
37
"django.contrib.sessions" ,
38
38
"django.contrib.messages" ,
39
39
"django.contrib.staticfiles" ,
40
+ "channels" , # Websocket library
40
41
]
41
42
42
43
MIDDLEWARE = [
123
124
# https://docs.djangoproject.com/en/3.2/ref/settings/#default-auto-field
124
125
125
126
DEFAULT_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 %}
1
2
<!DOCTYPE html>
2
3
< html lang ="en ">
3
4
4
5
< head >
5
6
< meta charset ="UTF-8 ">
6
7
< meta http-equiv ="X-UA-Compatible " content ="IE=edge ">
7
8
< meta name ="viewport " content ="width=device-width, initial-scale=1.0 ">
9
+ < script src ="{% static 'scripts.js' %} " crossorigin ="anonymous "> </ script >
8
10
< title > IDOM</ title >
9
11
</ head >
10
12
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