8 changed files with 101 additions and 58 deletions
@ -1,2 +1,3 @@ |
|||||
.env |
.env |
||||
__pycache__ |
__pycache__ |
||||
|
db.sqlite3 |
Binary file not shown.
@ -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), |
||||
|
), |
||||
|
] |
@ -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), |
||||
|
), |
||||
|
] |
@ -1,3 +1,61 @@ |
|||||
from django.shortcuts import render |
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. |
# 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') |
||||
|
Loading…
Reference in new issue