From e885f9decc8a50bae897bb69fb84289cd410c714 Mon Sep 17 00:00:00 2001 From: iou1name Date: Wed, 15 Aug 2018 17:41:51 -0400 Subject: [PATCH] groundwork for websockets in place (besides channel layers) --- jinja2/base.html | 1 + quest/consumers.py | 40 ++++++++++++++++++++++++++++++----- quest/events.py | 23 ++++++++++++++++++++ quest/jinja2/quest/quest.html | 16 +++++++------- quest/static/quest.js | 18 ++++++++++++++++ titivillus/asgi.py | 1 + 6 files changed, 86 insertions(+), 13 deletions(-) create mode 100644 quest/events.py diff --git a/jinja2/base.html b/jinja2/base.html index 38f2c57..b69d4fa 100644 --- a/jinja2/base.html +++ b/jinja2/base.html @@ -1,6 +1,7 @@ + {% block title %}{% endblock %} - Titivillus {% if request.user == quest.owner %} @@ -34,9 +34,9 @@ {% if post.post_type == "text" %}
{% elif post.post_type == "dice" %} -
+
{% elif post.post_type == "poll" %} -
+
{% endif %}
{{ post.timestamp.strftime('%Y-%m-%d %H:%M') }} @@ -44,9 +44,9 @@ {% if post.post_type == "text" %}
Edit - {% elif (post.post_type == "dice" or post.post_type == "poll") and post == posts|last %} -
- + {% elif post.post_type in ("dice", "poll") and post == posts|last %} +
+ {% endif %} {% endif %}
diff --git a/quest/static/quest.js b/quest/static/quest.js index 9e8a6cb..cebc831 100644 --- a/quest/static/quest.js +++ b/quest/static/quest.js @@ -1 +1,19 @@ var socket = new WebSocket('wss://' + document.domain + SCRIPT_NAME + '/ws/quest/' + quest_id + '/'); +socket.oldSend = socket.send; +socket.send = function(event_title, data) { + data = JSON.stringify({event: event_title, data: data}); + socket.oldSend.apply(this, [data]); +} +socket.events = {}; +socket.onmessage = function(e) { + let data = JSON.parse(e.data); + let event = data.event; + data = data.data; + if (socket.events[event] === undefined) { return; } + socket.events[event](data); +} + +socket.events['message'] = function(data) { + let message = data.message; + console.log(message); +} diff --git a/titivillus/asgi.py b/titivillus/asgi.py index 7e6c91f..9b4d2e7 100644 --- a/titivillus/asgi.py +++ b/titivillus/asgi.py @@ -4,6 +4,7 @@ ASGI entrypoint. Configures Django and then runs the application defined in the ASGI_APPLICATION setting. """ import os + import django from channels.routing import get_default_application