From 307de59b86a040efb284c7bad6158874296de8fe Mon Sep 17 00:00:00 2001 From: Edward Oliveira Date: Mon, 20 Sep 2021 22:59:56 -0300 Subject: [PATCH] web sockets (WIP) --- requirements/requirements.txt | 2 ++ sapl/asgi.py | 19 ++++++++++++ sapl/painel/consumers.py | 21 ++++++++++++++ sapl/painel/routing.py | 6 ++++ sapl/painel/urls.py | 5 +++- sapl/painel/views.py | 6 ++++ sapl/settings.py | 17 +++++++++++ sapl/templates/painel/room.html | 51 +++++++++++++++++++++++++++++++++ 8 files changed, 126 insertions(+), 1 deletion(-) create mode 100644 sapl/asgi.py create mode 100644 sapl/painel/consumers.py create mode 100644 sapl/painel/routing.py create mode 100644 sapl/templates/painel/room.html diff --git a/requirements/requirements.txt b/requirements/requirements.txt index bcfa70e1b..c8a02db81 100644 --- a/requirements/requirements.txt +++ b/requirements/requirements.txt @@ -35,3 +35,5 @@ whitenoise==5.1.0 git+https://github.com/interlegis/trml2pdf git+https://github.com/interlegis/django-admin-bootstrapped +channels==3.0.4 +channels-redis==3.3.0 diff --git a/sapl/asgi.py b/sapl/asgi.py new file mode 100644 index 000000000..63be23a6d --- /dev/null +++ b/sapl/asgi.py @@ -0,0 +1,19 @@ +import os + +import django +from channels.http import AsgiHandler +from channels.auth import AuthMiddlewareStack +from channels.routing import ProtocolTypeRouter, URLRouter +import sapl.painel.routing + +os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'sapl.settings') +django.setup() + +application = ProtocolTypeRouter({ + "http": AsgiHandler(), + "websocket": AuthMiddlewareStack( + URLRouter( + sapl.painel.routing.websocket_urlpatterns + ) + ), +}) \ No newline at end of file diff --git a/sapl/painel/consumers.py b/sapl/painel/consumers.py new file mode 100644 index 000000000..d987f39f2 --- /dev/null +++ b/sapl/painel/consumers.py @@ -0,0 +1,21 @@ +import json +from channels.generic.websocket import WebsocketConsumer + + +class ChatConsumer(WebsocketConsumer): + def connect(self): + self.accept() + print('conectado ao ws') + + def disconnect(self, close_code): + print('desconectado ao ws') + pass + + def receive(self, text_data): + print('receive message') + text_data_json = json.loads(text_data) + message = text_data_json['message'] + + self.send(text_data=json.dumps({ + 'message': message + })) \ No newline at end of file diff --git a/sapl/painel/routing.py b/sapl/painel/routing.py new file mode 100644 index 000000000..f5c85077a --- /dev/null +++ b/sapl/painel/routing.py @@ -0,0 +1,6 @@ +from django.urls import re_path +from . import consumers + +websocket_urlpatterns = [ + re_path(r'ws/painel/(?P\w+)/$', consumers.ChatConsumer.as_asgi()), +] \ No newline at end of file diff --git a/sapl/painel/urls.py b/sapl/painel/urls.py index 0795d0a35..ca7457bcf 100644 --- a/sapl/painel/urls.py +++ b/sapl/painel/urls.py @@ -1,9 +1,11 @@ from django.conf.urls import url +from django.urls import path + from .apps import AppConfig from .views import (cronometro_painel, get_dados_painel, painel_mensagem_view, painel_parlamentar_view, painel_view, painel_votacao_view, - switch_painel, verifica_painel, votante_view) + switch_painel, verifica_painel, votante_view, room) app_name = AppConfig.name @@ -24,4 +26,5 @@ urlpatterns = [ url(r'^voto-individual/$', votante_view, name='voto_individual'), + path(r'/', room, name='room'), ] diff --git a/sapl/painel/views.py b/sapl/painel/views.py index ed390f95b..8288d7ef7 100644 --- a/sapl/painel/views.py +++ b/sapl/painel/views.py @@ -643,3 +643,9 @@ def get_dados_painel(request, pk): # Retorna que não há nenhuma matéria já votada ou aberta return response_nenhuma_materia(get_presentes(pk, response, None)) + + +def room(request, room_name): + return render(request, 'painel/room.html', { + 'room_name': room_name + }) \ No newline at end of file diff --git a/sapl/settings.py b/sapl/settings.py index 8ddf5e4d7..9405985cb 100644 --- a/sapl/settings.py +++ b/sapl/settings.py @@ -96,6 +96,7 @@ INSTALLED_APPS = ( 'speedinfo', 'webpack_loader', + 'channels' ) + SAPL_APPS @@ -126,6 +127,20 @@ HAYSTACK_CONNECTIONS = { }, } +CHANNEL_LAYERS = { + 'default': { + ### Method 1: Via local Redis + # 'BACKEND': 'channels_redis.core.RedisChannelLayer', + # 'CONFIG': { + # "hosts": [('127.0.0.1', 6379)], + # }, + + ### Method 3: Via In-memory channel layer + ## Using this method. + "BACKEND": "channels.layers.InMemoryChannelLayer" + } +} + MIDDLEWARE = [ 'reversion.middleware.RevisionMiddleware', 'django.contrib.sessions.middleware.SessionMiddleware', @@ -203,6 +218,8 @@ TEMPLATES = [ WSGI_APPLICATION = 'sapl.wsgi.application' +ASGI_APPLICATION = 'sapl.asgi.application' + CELERY_BROKER_URL = 'redis://localhost:6379' diff --git a/sapl/templates/painel/room.html b/sapl/templates/painel/room.html new file mode 100644 index 000000000..2365f36f1 --- /dev/null +++ b/sapl/templates/painel/room.html @@ -0,0 +1,51 @@ + + + + + + Chat Room + + +
+
+ + {{ room_name|json_script:"room-name" }} + + + +