From 200d00e3f81ff12c5401941989b8b53b44d201fb Mon Sep 17 00:00:00 2001 From: Philipp Schmidt Date: Tue, 1 Aug 2017 20:49:24 +0200 Subject: [PATCH 1/2] add django channels plugin and basic websocket handler --- django/dkg/cancer/consumers.py | 10 ++++++++++ django/dkg/cancer/routing.py | 6 ++++++ django/dkg/cancer/templates/cancer/bs.html | 13 +++++++++++++ django/dkg/dkg/settings.py | 12 +++++++++++- django/requirements.txt | 3 ++- 5 files changed, 42 insertions(+), 2 deletions(-) create mode 100644 django/dkg/cancer/consumers.py create mode 100644 django/dkg/cancer/routing.py diff --git a/django/dkg/cancer/consumers.py b/django/dkg/cancer/consumers.py new file mode 100644 index 0000000..1181048 --- /dev/null +++ b/django/dkg/cancer/consumers.py @@ -0,0 +1,10 @@ +from django.http import HttpResponse +from channels.handler import AsgiHandler + + +def ws_message(message): + # ASGI WebSocket packet-received and send-packet message types + # both have a "text" key for their textual data. + message.reply_channel.send({ + "text": message.content['text'], + }) diff --git a/django/dkg/cancer/routing.py b/django/dkg/cancer/routing.py new file mode 100644 index 0000000..9031236 --- /dev/null +++ b/django/dkg/cancer/routing.py @@ -0,0 +1,6 @@ +from channels.routing import route +from cancer.consumers import ws_message + +channel_routing = [ + route("websocket.receive", ws_message), +] diff --git a/django/dkg/cancer/templates/cancer/bs.html b/django/dkg/cancer/templates/cancer/bs.html index a36fe36..c7f980f 100644 --- a/django/dkg/cancer/templates/cancer/bs.html +++ b/django/dkg/cancer/templates/cancer/bs.html @@ -25,6 +25,19 @@ integrity="sha384-Tc5IQib027qvyjSMfHjOMaLkfuWVxZxUPnCJA7l2mCWNIpG9mGCD8wGNIcPD7Txa" crossorigin="anonymous"> +