Update first commit
This commit is contained in:
0
apps/back/__init__.py
Normal file
0
apps/back/__init__.py
Normal file
3
apps/back/admin.py
Normal file
3
apps/back/admin.py
Normal file
@ -0,0 +1,3 @@
|
||||
from django.contrib import admin
|
||||
|
||||
# Register your models here.
|
5
apps/back/apps.py
Normal file
5
apps/back/apps.py
Normal file
@ -0,0 +1,5 @@
|
||||
from django.apps import AppConfig
|
||||
|
||||
|
||||
class BackConfig(AppConfig):
|
||||
name = 'back'
|
55
apps/back/consumers.py
Normal file
55
apps/back/consumers.py
Normal file
@ -0,0 +1,55 @@
|
||||
import json
|
||||
from channels.generic.websocket import AsyncWebsocketConsumer
|
||||
from asgiref.sync import sync_to_async
|
||||
|
||||
class BackConsumer(AsyncWebsocketConsumer):
|
||||
|
||||
def connect(self):
|
||||
''' Cliente se conecta '''
|
||||
|
||||
# Recoge el nombre de la sala
|
||||
self.room_name = self.scope["url_route"]["kwargs"]["room_name"]
|
||||
self.room_group_name = "blog_%s" % self.room_name
|
||||
|
||||
# Se une a la sala
|
||||
await self.channel_layer.group_add(self.room_group_name, self.channel_name)
|
||||
|
||||
# Informa al cliente del éxito
|
||||
await self.accept()
|
||||
|
||||
def disconnect(self, close_code):
|
||||
''' Cliente se desconecta '''
|
||||
# Leave room group
|
||||
await self.channel_layer.group_discard(self.room_group_name, self.channel_name)
|
||||
|
||||
def receive(self, text_data):
|
||||
''' Cliente envía información y nosotros la recibimos '''
|
||||
text_data_json = json.loads(text_data)
|
||||
name = text_data_json["name"]
|
||||
text = text_data_json["text"]
|
||||
|
||||
# Enviamos el mensaje a la sala
|
||||
await self.channel_layer.group_send(
|
||||
self.room_group_name,
|
||||
{
|
||||
"type": "chat_message",
|
||||
"name": name,
|
||||
"text": text
|
||||
}
|
||||
)
|
||||
|
||||
def chat_message(self, event):
|
||||
''' Recibimos información de la sala '''
|
||||
name = event["name"]
|
||||
text = event["text"]
|
||||
|
||||
# Send message to WebSocket
|
||||
await self.send(
|
||||
text_data=json.dumps(
|
||||
{
|
||||
"type": "chat_message",
|
||||
"name": name,
|
||||
"text": text
|
||||
}
|
||||
)
|
||||
)
|
0
apps/back/migrations/__init__.py
Normal file
0
apps/back/migrations/__init__.py
Normal file
3
apps/back/models.py
Normal file
3
apps/back/models.py
Normal file
@ -0,0 +1,3 @@
|
||||
from django.db import models
|
||||
|
||||
# Create your models here.
|
7
apps/back/routing.py
Normal file
7
apps/back/routing.py
Normal file
@ -0,0 +1,7 @@
|
||||
from django.urls import re_path
|
||||
|
||||
from . import consumers
|
||||
|
||||
websocket_urlpatterns = [
|
||||
re_path(r'ws/blog/(?P<room_name>\w+)/$', consumers.BlogConsumer),
|
||||
]
|
3
apps/back/views.py
Normal file
3
apps/back/views.py
Normal file
@ -0,0 +1,3 @@
|
||||
from django.shortcuts import render
|
||||
|
||||
# Create your views here.
|
Reference in New Issue
Block a user