Skip to content

Commit 923276e

Browse files
committed
barebones unauthenticated websocket
1 parent 1591eb1 commit 923276e

File tree

5 files changed

+53
-11
lines changed

5 files changed

+53
-11
lines changed

dj_idom/asgi.py

Lines changed: 1 addition & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -17,19 +17,13 @@
1717
# Fetch ASGI application before importing dependencies that require ORM models.
1818
http_asgi_app = get_asgi_application()
1919

20-
from channels.auth import AuthMiddlewareStack
2120
from channels.routing import ProtocolTypeRouter, URLRouter
22-
from channels.security.websocket import AllowedHostsOriginValidator
2321

2422
from .consumers import CommandConsumer
2523

2624
application = 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
)

dj_idom/settings.py

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@
3737
"django.contrib.sessions",
3838
"django.contrib.messages",
3939
"django.contrib.staticfiles",
40+
"channels", # Websocket library
4041
]
4142

4243
MIDDLEWARE = [
@@ -123,3 +124,8 @@
123124
# https://docs.djangoproject.com/en/3.2/ref/settings/#default-auto-field
124125

125126
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+
]

dj_idom/static/scripts.js

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
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+
};

dj_idom/templates/base.html

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,12 @@
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

requirements.txt

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
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

0 commit comments

Comments
 (0)