diff --git a/django/requirements.txt b/django/requirements.txt index 8e7901e..7172056 100644 --- a/django/requirements.txt +++ b/django/requirements.txt @@ -8,3 +8,6 @@ psycopg2==2.9.9 celery==5.2.7 redis==3.5.3 python-dotenv==1.0.1 +channels==4.1.0 +daphne==4.1.2 +python-socketio==5.11.4 diff --git a/django/tts_be/asgi.py b/django/tts_be/asgi.py index 54873d2..88764b8 100644 --- a/django/tts_be/asgi.py +++ b/django/tts_be/asgi.py @@ -8,9 +8,13 @@ """ import os - from django.core.asgi import get_asgi_application +import socketio +from university.socket.views import sio + os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'tts_be.settings') -application = get_asgi_application() +django_app = get_asgi_application() + +application = socketio.ASGIApp(sio, django_app) diff --git a/django/tts_be/settings.py b/django/tts_be/settings.py index 2966293..8aa93d4 100644 --- a/django/tts_be/settings.py +++ b/django/tts_be/settings.py @@ -24,7 +24,8 @@ SECRET_KEY = os.getenv('SECRET_KEY') -DEBUG = False if int(os.getenv('DEBUG')) == 0 else True +DEBUG = os.getenv('DEBUG') +DEBUG = int(DEBUG) != 0 if DEBUG else False ALLOWED_HOSTS = ['0.0.0.0', 'localhost', 'tts.niaefeup.pt', 'tts-staging.niaefeup.pt'] @@ -32,6 +33,7 @@ INSTALLED_APPS = [ 'corsheaders', + 'daphne', 'django.contrib.admin', 'django.contrib.auth', 'django.contrib.contenttypes', @@ -40,6 +42,7 @@ 'rest_framework', 'django.contrib.staticfiles', 'university', + 'channels', ] MIDDLEWARE = [ @@ -74,6 +77,8 @@ WSGI_APPLICATION = 'tts_be.wsgi.application' +ASGI_APPLICATION = 'tts_be.asgi.application' + # Database # https://docs.djangoproject.com/en/4.0/ref/settings/#databases diff --git a/django/university/socket/sessionsserver.py b/django/university/socket/sessionsserver.py new file mode 100644 index 0000000..1414b46 --- /dev/null +++ b/django/university/socket/sessionsserver.py @@ -0,0 +1,11 @@ +import uuid + +class SessionsServer: + def __init__(self, sio): + self.sio = sio + + def valid_token(self, token): + return True + + def event(self, event): + return self.sio.event(event) \ No newline at end of file diff --git a/django/university/socket/views.py b/django/university/socket/views.py new file mode 100644 index 0000000..ca0d395 --- /dev/null +++ b/django/university/socket/views.py @@ -0,0 +1,33 @@ +import os +import socketio + +from university.socket.sessionsserver import SessionsServer + +async_mode = None + +basedir = os.path.dirname(os.path.realpath(__file__)) +sio = socketio.AsyncServer( + async_mode='asgi', + cors_allowed_origins="*", +) + +session_server = SessionsServer(sio) + +@session_server.event +async def connect(sid, environ, auth): + if auth is None or 'token' not in auth: + raise ConnectionRefusedError('Authentication failed: No token provided') + elif not session_server.valid_token(auth['token']): + raise ConnectionRefusedError('Authentication failed: Invalid token') + else: + print('Client connected') + await sio.emit('my_response', {'data': 'Connected', 'count': 0}, room=sid) + + +@session_server.event +def disconnect(sid): + print('Client disconnected') + +@session_server.event +async def test(sid, environ): + await sio.emit('response', 'ya') \ No newline at end of file