diff --git a/examples/server/asgi/django_example/.gitignore b/examples/server/asgi/django_example/.gitignore index dc12cb7..940f655 100644 --- a/examples/server/asgi/django_example/.gitignore +++ b/examples/server/asgi/django_example/.gitignore @@ -1,2 +1,3 @@ .env -__pycache__ \ No newline at end of file +__pycache__ +db.sqlite3 \ No newline at end of file diff --git a/examples/server/asgi/django_example/README.md b/examples/server/asgi/django_example/README.md index 43ba9dc..1a87f81 100644 --- a/examples/server/asgi/django_example/README.md +++ b/examples/server/asgi/django_example/README.md @@ -5,4 +5,6 @@ (in virtual environment) - python server.py -to start app \ No newline at end of file +to start app + +- The Procfile contains settings for running uvicorn in deployment with gunicorn as stated in the django documentation \ No newline at end of file diff --git a/examples/server/asgi/django_example/core/asgi.py b/examples/server/asgi/django_example/core/asgi.py index 77148a5..4af44ba 100644 --- a/examples/server/asgi/django_example/core/asgi.py +++ b/examples/server/asgi/django_example/core/asgi.py @@ -17,68 +17,15 @@ django_asgi_app = get_asgi_application() #its important to make all other imports below this comment import socketio -from django_io.models import Message -from django_io.serializers import message_serializer -from django_io.utils import generate_short_id -from socketio.exceptions import ConnectionRefusedError -from datetime import datetime, timezone -from asgiref.sync import sync_to_async -import json +from django_io.views import sio from dotenv import load_dotenv load_dotenv() -mgr = socketio.AsyncRedisManager(os.getenv('REDIS_URL')) -sio = socketio.AsyncServer(async_mode="asgi", client_manager=mgr, cors_allowed_origins="*") -application = socketio.ASGIApp(sio, django_asgi_app) - -# application = get_asgi_application() - - -#establishes a connection with the client -@sio.on("connect") -async def connect(sid, env, auth): - if auth: - print("SocketIO connect") - sio.enter_room(sid, "feed") - await sio.emit("connect", f"Connected as {sid}") - -#communication with orm -def store_and_return_message(data): - data = json.loads(data) - instance = Message.objects.create( - author = data["username"], - message = data["message"] - ) - instance.save() - message = message_serializer(instance) - return message - - - -# listening to a 'message' event from the client -@sio.on('message') -async def print_message(sid, data): - print("Socket ID", sid) - message = await sync_to_async(store_and_return_message, thread_sensitive=True)(data) #communicating with orm - print(message) - await sio.emit("new_message", message, room="feed") +application = socketio.ASGIApp(sio, django_asgi_app) -@sio.on("disconnect") -async def disconnect(sid): - print("SocketIO disconnect") -#extra events -@sio.on('left') -async def left_room(sid, data): - sio.leave_room(sid, "feed") - await sio.emit("user_left", f'{data} left', room="feed") - print(f'{sid} Left') -@sio.on("joined") -async def joined(sid, data): - await sio.emit("user_joined", f'{data} connected', room="feed") - print(f'{data} connected') diff --git a/examples/server/asgi/django_example/core/settings.py b/examples/server/asgi/django_example/core/settings.py index 3821326..adb1ecc 100644 --- a/examples/server/asgi/django_example/core/settings.py +++ b/examples/server/asgi/django_example/core/settings.py @@ -37,7 +37,6 @@ INSTALLED_APPS = [ 'django.contrib.sessions', 'django.contrib.messages', 'django.contrib.staticfiles', - 'socketio', 'django_io' ] diff --git a/examples/server/asgi/django_example/db.sqlite3 b/examples/server/asgi/django_example/db.sqlite3 deleted file mode 100644 index a308e36..0000000 Binary files a/examples/server/asgi/django_example/db.sqlite3 and /dev/null differ diff --git a/examples/server/asgi/django_example/django_io/migrations/0002_alter_message_short_id.py b/examples/server/asgi/django_example/django_io/migrations/0002_alter_message_short_id.py new file mode 100644 index 0000000..9820967 --- /dev/null +++ b/examples/server/asgi/django_example/django_io/migrations/0002_alter_message_short_id.py @@ -0,0 +1,18 @@ +# Generated by Django 4.0.1 on 2022-01-10 19:57 + +from django.db import migrations, models + + +class Migration(migrations.Migration): + + dependencies = [ + ('django_io', '0001_initial'), + ] + + operations = [ + migrations.AlterField( + model_name='message', + name='short_id', + field=models.CharField(default='09MCV8R2B', max_length=255), + ), + ] diff --git a/examples/server/asgi/django_example/django_io/migrations/0003_alter_message_short_id.py b/examples/server/asgi/django_example/django_io/migrations/0003_alter_message_short_id.py new file mode 100644 index 0000000..a25ad17 --- /dev/null +++ b/examples/server/asgi/django_example/django_io/migrations/0003_alter_message_short_id.py @@ -0,0 +1,18 @@ +# Generated by Django 4.0.1 on 2022-01-10 20:00 + +from django.db import migrations, models + + +class Migration(migrations.Migration): + + dependencies = [ + ('django_io', '0002_alter_message_short_id'), + ] + + operations = [ + migrations.AlterField( + model_name='message', + name='short_id', + field=models.CharField(default='KKFN5ACTH', max_length=255), + ), + ] diff --git a/examples/server/asgi/django_example/django_io/views.py b/examples/server/asgi/django_example/django_io/views.py index 91ea44a..45e2f1f 100644 --- a/examples/server/asgi/django_example/django_io/views.py +++ b/examples/server/asgi/django_example/django_io/views.py @@ -1,3 +1,61 @@ from django.shortcuts import render +import json +import os +from .models import Message +from .serializers import message_serializer +from asgiref.sync import sync_to_async +import socketio +from dotenv import load_dotenv +load_dotenv() +mgr = socketio.AsyncRedisManager(os.getenv('REDIS_URL')) +sio = socketio.AsyncServer(async_mode="asgi", client_manager=mgr, cors_allowed_origins="*") # Create your views here. + +#establishes a connection with the client +@sio.on("connect") +async def connect(sid, env, auth): + if auth: + print("SocketIO connect") + sio.enter_room(sid, "feed") + await sio.emit("connect", f"Connected as {sid}") + +#communication with orm +def store_and_return_message(data): + data = json.loads(data) + instance = Message.objects.create( + author = data["username"], + message = data["message"] + ) + instance.save() + message = message_serializer(instance) + return message + + + +# listening to a 'message' event from the client +@sio.on('mess') +async def print_message(sid, data): + print("Socket ID", sid) + message = await sync_to_async(store_and_return_message, thread_sensitive=True)(data) #communicating with orm + print(message) + await sio.emit("new_message", message, room="feed") + + + +@sio.on("disconnect") +async def disconnect(sid): + print("SocketIO disconnect") + + +#extra events +@sio.on('left') +async def left_room(sid, data): + sio.leave_room(sid, "feed") + await sio.emit("user_left", f'{data} left', room="feed") + print(f'{sid} Left') + +@sio.on("joined") +async def joined(sid, data): + await sio.emit("user_joined", f'{data} connected', room="feed") + print(f'{data} connected')