diff --git a/SAS/TMSS/src/tmss/CMakeLists.txt b/SAS/TMSS/src/tmss/CMakeLists.txt index 3e7754777f2f6d34a58352c9d78765303dd9cfa4..619ee0ca7f39dbe1755a12fcbeb2f91ab30e66d5 100644 --- a/SAS/TMSS/src/tmss/CMakeLists.txt +++ b/SAS/TMSS/src/tmss/CMakeLists.txt @@ -3,6 +3,7 @@ include(PythonInstall) set(_py_files __init__.py + routing.py settings.py urls.py wsgi.py diff --git a/SAS/TMSS/src/tmss/routing.py b/SAS/TMSS/src/tmss/routing.py new file mode 100644 index 0000000000000000000000000000000000000000..d8a9711653d45b13fb53def578aa706c54e67d19 --- /dev/null +++ b/SAS/TMSS/src/tmss/routing.py @@ -0,0 +1,15 @@ +from django.urls import re_path + +from channels.routing import ProtocolTypeRouter, URLRouter +from channels.auth import AuthMiddlewareStack + +from .tmssapp import consumers + +application = ProtocolTypeRouter({ + # Websocket handler + "websocket": AuthMiddlewareStack( + URLRouter([ + re_path(r"ws/$", consumers.AppConsumer.as_asgi()), + ]) + ), +}) \ No newline at end of file diff --git a/SAS/TMSS/src/tmss/settings.py b/SAS/TMSS/src/tmss/settings.py index d5a948d2878938f25e4ba512f8c03930cbcea036..9a1c3fda21ce866c8724f3a79722877b4d41db03 100644 --- a/SAS/TMSS/src/tmss/settings.py +++ b/SAS/TMSS/src/tmss/settings.py @@ -125,6 +125,7 @@ INSTALLED_APPS = [ 'viewflow', 'viewflow.frontend', 'lofar.sas.tmss.tmss.workflowapp', + 'channels', ] @@ -172,8 +173,15 @@ STATICFILES_DIRS = [ os.path.join(BASE_DIR, '../frontend','tmss_webapp/build/static') ] +ASGI_APPLICATION = 'lofar.sas.tmss.tmss.routing.application' WSGI_APPLICATION = 'lofar.sas.tmss.tmss.wsgi.application' +CHANNEL_LAYERS = { + "default": { + "BACKEND": "channels.layers.InMemoryChannelLayer", + }, +} + # Database # https://docs.djangoproject.com/en/2.0/ref/settings/#databases diff --git a/SAS/TMSS/src/tmss/tmssapp/CMakeLists.txt b/SAS/TMSS/src/tmss/tmssapp/CMakeLists.txt index 58c545f7ed434d8c05064e1fad48ebf0c93d821a..40bd68ede8ca333682c7204997fb0c4f9fcca7a9 100644 --- a/SAS/TMSS/src/tmss/tmssapp/CMakeLists.txt +++ b/SAS/TMSS/src/tmss/tmssapp/CMakeLists.txt @@ -10,6 +10,7 @@ set(_py_files subtasks.py tasks.py conversions.py + consumers.py ) python_install(${_py_files} diff --git a/SAS/TMSS/src/tmss/tmssapp/__init__.py b/SAS/TMSS/src/tmss/tmssapp/__init__.py index e69de29bb2d1d6434b8b29ae775ad8c2e48c5391..cd7385e40fde94092a2b4657b0889d855597967c 100644 --- a/SAS/TMSS/src/tmss/tmssapp/__init__.py +++ b/SAS/TMSS/src/tmss/tmssapp/__init__.py @@ -0,0 +1 @@ +from .consumers import * diff --git a/SAS/TMSS/src/tmss/tmssapp/consumers.py b/SAS/TMSS/src/tmss/tmssapp/consumers.py new file mode 100644 index 0000000000000000000000000000000000000000..c6d1a47ec5b0ff6367b047d509fb1ab3b0b9767d --- /dev/null +++ b/SAS/TMSS/src/tmss/tmssapp/consumers.py @@ -0,0 +1,22 @@ +from channels.generic.websocket import WebsocketConsumer +import json + + +# Simple Channels consumer for Websockets +class AppConsumer(WebsocketConsumer): + + def connect(self): + self.accept() + + def disconnect(self, close_code): + pass + + def receive(self, text_data): + text_data_json = json.loads(text_data) + message = text_data_json['message'] + + print('WS> Received', message) + + self.send(text_data=json.dumps({ + 'message': message[::-1] + })) diff --git a/SAS/TMSS/src/tmss/tmssapp/views.py b/SAS/TMSS/src/tmss/tmssapp/views.py index 58a389fd6e332c7fea88e113fda8fe8e0d734217..18737121aa17d40d9e1d8d92c949c1601494a522 100644 --- a/SAS/TMSS/src/tmss/tmssapp/views.py +++ b/SAS/TMSS/src/tmss/tmssapp/views.py @@ -109,4 +109,8 @@ def lst(request): lst_lon = local_sidereal_time_for_utc_and_station(timestamp) # todo: do we want to return a dict, so users can make sure their parameters were parsed correctly instead? - return HttpResponse(str(lst_lon), content_type='text/plain') \ No newline at end of file + return HttpResponse(str(lst_lon), content_type='text/plain') + + +def ws_test(request): + return render(request, os.path.join(os.path.dirname(os.path.dirname(os.path.realpath(__file__))), 'tmssapp/ws.html')) diff --git a/SAS/TMSS/src/tmss/tmssapp/ws.html b/SAS/TMSS/src/tmss/tmssapp/ws.html new file mode 100644 index 0000000000000000000000000000000000000000..aa04558ff523699c759f4636c09f6c07d3df0909 --- /dev/null +++ b/SAS/TMSS/src/tmss/tmssapp/ws.html @@ -0,0 +1,34 @@ +<!DOCTYPE html> +<html> + +<head> + <meta charset="utf-8" /> + <title>Websocket Test</title> +</head> + +<body> + + <script> + let chatSocket = new WebSocket( + 'ws://' + window.location.host + + '/ws/'); + + chatSocket.onmessage = (e) => { + const data = JSON.parse(e.data); + const message = data['message']; + console.log(message); + }; + + chatSocket.onclose = (e) => { + console.error('Websocket closed unexpectedly'); + }; + + const send = (msg, e) => { + chatSocket.send(JSON.stringify({ + 'message': msg + })); + }; + </script> +</body> + +</html> \ No newline at end of file diff --git a/SAS/TMSS/src/tmss/urls.py b/SAS/TMSS/src/tmss/urls.py index 376c1c83ba457c81f98901a262b286e8db94c52b..807ef6e6bfbbb4a9a8c88af8aa9bfefb713f0286 100644 --- a/SAS/TMSS/src/tmss/urls.py +++ b/SAS/TMSS/src/tmss/urls.py @@ -67,6 +67,7 @@ urlpatterns = [ path('station_groups/<str:template_name>/<str:template_version>/<str:station_group>/', views.get_stations_in_group, name='get_stations_in_group'), path(r'util/utc', views.utc, name="system-utc"), path(r'util/lst', views.lst, name="conversion-lst"), + path('ws/', views.ws_test, name="ws_test"), ] if os.environ.get('SHOW_DJANGO_DEBUG_TOOLBAR', False):