From 655bffea1f4798057d8bbebe4dcdc3f149be7c24 Mon Sep 17 00:00:00 2001 From: Psami-wondah Date: Mon, 10 Jan 2022 21:06:20 +0100 Subject: [PATCH] Moved the sio application to views.py, deleted the reference to the socketio app that doesn't exist --- .../server/asgi/django_example/.gitignore | 3 +- examples/server/asgi/django_example/README.md | 4 +- .../server/asgi/django_example/core/asgi.py | 57 +---------------- .../asgi/django_example/core/settings.py | 1 - .../server/asgi/django_example/db.sqlite3 | Bin 135168 -> 0 bytes .../migrations/0002_alter_message_short_id.py | 18 ++++++ .../migrations/0003_alter_message_short_id.py | 18 ++++++ .../asgi/django_example/django_io/views.py | 58 ++++++++++++++++++ 8 files changed, 101 insertions(+), 58 deletions(-) delete mode 100644 examples/server/asgi/django_example/db.sqlite3 create mode 100644 examples/server/asgi/django_example/django_io/migrations/0002_alter_message_short_id.py create mode 100644 examples/server/asgi/django_example/django_io/migrations/0003_alter_message_short_id.py 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 a308e3600651e7ed33b1e196011a943bee04fc92..0000000000000000000000000000000000000000 GIT binary patch literal 0 HcmV?d00001 literal 135168 zcmeI5eQX=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|7lWOp|Eh!UQpvp9q=qT;`_4jsoLG0aK@|fT`74AU;M4%wf8iszXrW3F009sH0T2KI5C8!X009sH0T2Lz4}pN={r^L#4OM^u z2!H?xfB*=900@8p2!H?xfB*=56a?t{|A0{R34bm868VD<2!H?xfB*=900@8p2!H?x zfB*=9z`h8i17Y9e;?i`ZS=X!U+uOPA#bhFxoKMK}azc{j3z_tVRQ7x}m(C?J*;_Ym zym)oBkVyJTsR^spZEf3CCUqf|IiF5uGWkWj4CgF!U#q*yBrYU#=X0q{W--++^MA8B BW>^3K 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')