From 67a65702a12967d7e1319b78edca24f01a3788cd Mon Sep 17 00:00:00 2001 From: iou1name Date: Tue, 14 Aug 2018 20:12:52 -0400 Subject: [PATCH] preliminary websocket support added --- README.md | 1 + quest/consumers.py | 20 ++++++++++++++++++++ quest/jinja2/quest/quest.html | 12 ++++++++---- quest/routing.py | 11 +++++++++++ quest/static/quest.css | 2 +- quest/static/quest.js | 1 + titivillus/asgi.py | 12 ++++++++++++ titivillus/routing.py | 16 ++++++++++++++++ titivillus/settings.py | 3 +++ 9 files changed, 73 insertions(+), 5 deletions(-) create mode 100644 quest/consumers.py create mode 100644 quest/routing.py create mode 100644 quest/static/quest.js create mode 100644 titivillus/asgi.py create mode 100644 titivillus/routing.py diff --git a/README.md b/README.md index 1caa97f..908ab7e 100644 --- a/README.md +++ b/README.md @@ -24,3 +24,4 @@ postgres=# \q ## Usage `gunicorn -b localhost:5100 -e SCRIPT_NAME=/titivillus titivillus.wsgi` +`daphne -b 0.0.0.0 -p 5100 --root-path=/titivillus titivillus.asgi:application` diff --git a/quest/consumers.py b/quest/consumers.py new file mode 100644 index 0000000..afdc768 --- /dev/null +++ b/quest/consumers.py @@ -0,0 +1,20 @@ +#!/usr/bin/env python3 +""" +Consumers available for the /quest websocket. +""" +from channels.generic.websocket import WebsocketConsumer + +class QuestConsumer(WebsocketConsumer): + """ + The main consumer for /quest websockets. + """ + def connect(self): + self.accept() + self.send('message: connected') + + def disconnect(self): + pass + + def receive(self, text_data): + print(text_data) + self.send("message: lol}") diff --git a/quest/jinja2/quest/quest.html b/quest/jinja2/quest/quest.html index f5756aa..a64271e 100644 --- a/quest/jinja2/quest/quest.html +++ b/quest/jinja2/quest/quest.html @@ -1,11 +1,15 @@ {% extends "base.html" %} {% block title %}{{ quest.title }}{% endblock %} {% block head %} - {##} - - {##} + + + {% if request.user == quest.owner %} - {##} + {##} {% endif %} {##} {% endblock %} diff --git a/quest/routing.py b/quest/routing.py new file mode 100644 index 0000000..4c58e3e --- /dev/null +++ b/quest/routing.py @@ -0,0 +1,11 @@ +#!/usr/bin/env python3 +""" +Routing for /quest websockets. +""" +from django.conf.urls import url + +from . import consumers + +websocket_urlpatterns = [ + url(r'ws/quest/(?P\d+)/', consumers.QuestConsumer), +] diff --git a/quest/static/quest.css b/quest/static/quest.css index 14c79d1..9b44446 100644 --- a/quest/static/quest.css +++ b/quest/static/quest.css @@ -16,7 +16,7 @@ h3 { #questPane { padding-left: 5%; padding-right: 35%; - min-width: 0; + width: 100%; } .questPost { diff --git a/quest/static/quest.js b/quest/static/quest.js new file mode 100644 index 0000000..9e8a6cb --- /dev/null +++ b/quest/static/quest.js @@ -0,0 +1 @@ +var socket = new WebSocket('wss://' + document.domain + SCRIPT_NAME + '/ws/quest/' + quest_id + '/'); diff --git a/titivillus/asgi.py b/titivillus/asgi.py new file mode 100644 index 0000000..7e6c91f --- /dev/null +++ b/titivillus/asgi.py @@ -0,0 +1,12 @@ +#!/usr/bin/env python3 +""" +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 + +os.environ.setdefault("DJANGO_SETTINGS_MODULE", "titivillus.settings") +django.setup() +application = get_default_application() diff --git a/titivillus/routing.py b/titivillus/routing.py new file mode 100644 index 0000000..d506dd2 --- /dev/null +++ b/titivillus/routing.py @@ -0,0 +1,16 @@ +#!/usr/bin/env python3 +""" +Routing for Channels (websockets). +""" +from channels.auth import AuthMiddlewareStack +from channels.routing import ProtocolTypeRouter, URLRouter + +import quest.routing + +application = ProtocolTypeRouter({ + 'websocket': AuthMiddlewareStack( + URLRouter( + quest.routing.websocket_urlpatterns + ) + ), +}) diff --git a/titivillus/settings.py b/titivillus/settings.py index 550ce1e..f3a0906 100644 --- a/titivillus/settings.py +++ b/titivillus/settings.py @@ -31,6 +31,7 @@ INSTALLED_APPS = [ 'django.contrib.sessions', 'django.contrib.messages', 'django.contrib.staticfiles', + 'channels', 'users.apps.UsersConfig', 'homepage.apps.HomepageConfig', 'create_quest.apps.CreateQuestConfig', @@ -144,3 +145,5 @@ PASSWORD_HASHERS = [ 'django.contrib.auth.hashers.PBKDF2SHA1PasswordHasher', 'django.contrib.auth.hashers.BCryptSHA256PasswordHasher', ] + +ASGI_APPLICATION = 'titivillus.routing.application'