Skip to content
Snippets Groups Projects
Commit 2950e40f authored by mario's avatar mario
Browse files

TMSS-413: Add Websockets files

parent 7999e53f
No related branches found
No related tags found
1 merge request!282Resolve TMSS-417
......@@ -3,6 +3,7 @@ include(PythonInstall)
set(_py_files
__init__.py
routing.py
settings.py
urls.py
wsgi.py
......
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
......@@ -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
......
......@@ -10,6 +10,7 @@ set(_py_files
subtasks.py
tasks.py
conversions.py
consumers.py
)
python_install(${_py_files}
......
from .consumers import *
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]
}))
......@@ -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'))
<!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
......@@ -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):
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment