preliminary websocket support added

This commit is contained in:
iou1name 2018-08-14 20:12:52 -04:00
parent 20bab88d04
commit 67a65702a1
9 changed files with 73 additions and 5 deletions

View File

@ -24,3 +24,4 @@ postgres=# \q
## Usage ## Usage
`gunicorn -b localhost:5100 -e SCRIPT_NAME=/titivillus titivillus.wsgi` `gunicorn -b localhost:5100 -e SCRIPT_NAME=/titivillus titivillus.wsgi`
`daphne -b 0.0.0.0 -p 5100 --root-path=/titivillus titivillus.asgi:application`

20
quest/consumers.py Normal file
View File

@ -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}")

View File

@ -1,11 +1,15 @@
{% extends "base.html" %} {% extends "base.html" %}
{% block title %}{{ quest.title }}{% endblock %} {% block title %}{{ quest.title }}{% endblock %}
{% block head %} {% block head %}
{#<script type="text/javascript" src="/static/socket.io.slim.js"></script>#} <link rel="stylesheet" type="text/css" href="{{ static('quest.css') }}">
<script>var quest_id = {{ quest.id }}; var page_num = {{ page_num }};</script> <script>
{#<script type="text/javascript" src="/static/anonkunUser.js"></script>#} var quest_id = {{ quest.id }};
var page_num = {{ page_num }};
var SCRIPT_NAME = '{{ request.META["SCRIPT_NAME"] }}';
</script>
<script type="text/javascript" src="{{ static('quest.js') }}"></script>
{% if request.user == quest.owner %} {% if request.user == quest.owner %}
{#<script type="text/javascript" src="/static/anonkunQM.js"></script>#} {#<script type="text/javascript" src="{{ static('questQM.js') }}"></script>#}
{% endif %} {% endif %}
{#<script>window.onload = load;</script>#} {#<script>window.onload = load;</script>#}
{% endblock %} {% endblock %}

11
quest/routing.py Normal file
View File

@ -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<quest_id>\d+)/', consumers.QuestConsumer),
]

View File

@ -16,7 +16,7 @@ h3 {
#questPane { #questPane {
padding-left: 5%; padding-left: 5%;
padding-right: 35%; padding-right: 35%;
min-width: 0; width: 100%;
} }
.questPost { .questPost {

1
quest/static/quest.js Normal file
View File

@ -0,0 +1 @@
var socket = new WebSocket('wss://' + document.domain + SCRIPT_NAME + '/ws/quest/' + quest_id + '/');

12
titivillus/asgi.py Normal file
View File

@ -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()

16
titivillus/routing.py Normal file
View File

@ -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
)
),
})

View File

@ -31,6 +31,7 @@ INSTALLED_APPS = [
'django.contrib.sessions', 'django.contrib.sessions',
'django.contrib.messages', 'django.contrib.messages',
'django.contrib.staticfiles', 'django.contrib.staticfiles',
'channels',
'users.apps.UsersConfig', 'users.apps.UsersConfig',
'homepage.apps.HomepageConfig', 'homepage.apps.HomepageConfig',
'create_quest.apps.CreateQuestConfig', 'create_quest.apps.CreateQuestConfig',
@ -144,3 +145,5 @@ PASSWORD_HASHERS = [
'django.contrib.auth.hashers.PBKDF2SHA1PasswordHasher', 'django.contrib.auth.hashers.PBKDF2SHA1PasswordHasher',
'django.contrib.auth.hashers.BCryptSHA256PasswordHasher', 'django.contrib.auth.hashers.BCryptSHA256PasswordHasher',
] ]
ASGI_APPLICATION = 'titivillus.routing.application'