Skip to content

Commit

Permalink
Merge pull request #115 from NIAEFEUP/feature/websockets
Browse files Browse the repository at this point in the history
Add WebSocket integration
  • Loading branch information
Process-ing authored Nov 10, 2024
2 parents 48c90ba + dec36e5 commit a651a5d
Show file tree
Hide file tree
Showing 5 changed files with 59 additions and 3 deletions.
3 changes: 3 additions & 0 deletions django/requirements.txt
Original file line number Diff line number Diff line change
Expand Up @@ -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
8 changes: 6 additions & 2 deletions django/tts_be/asgi.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)
7 changes: 6 additions & 1 deletion django/tts_be/settings.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,14 +24,16 @@

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']

# Application definition

INSTALLED_APPS = [
'corsheaders',
'daphne',
'django.contrib.admin',
'django.contrib.auth',
'django.contrib.contenttypes',
Expand All @@ -40,6 +42,7 @@
'rest_framework',
'django.contrib.staticfiles',
'university',
'channels',
]

MIDDLEWARE = [
Expand Down Expand Up @@ -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
Expand Down
11 changes: 11 additions & 0 deletions django/university/socket/sessionsserver.py
Original file line number Diff line number Diff line change
@@ -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)
33 changes: 33 additions & 0 deletions django/university/socket/views.py
Original file line number Diff line number Diff line change
@@ -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')

0 comments on commit a651a5d

Please sign in to comment.