add
This commit is contained in:
0
apps/front/__init__.py
Normal file
0
apps/front/__init__.py
Normal file
3
apps/front/admin.py
Normal file
3
apps/front/admin.py
Normal file
@ -0,0 +1,3 @@
|
||||
from django.contrib import admin
|
||||
|
||||
# Register your models here.
|
5
apps/front/apps.py
Normal file
5
apps/front/apps.py
Normal file
@ -0,0 +1,5 @@
|
||||
from django.apps import AppConfig
|
||||
|
||||
|
||||
class FrontConfig(AppConfig):
|
||||
name = 'front'
|
0
apps/front/migrations/__init__.py
Normal file
0
apps/front/migrations/__init__.py
Normal file
3
apps/front/models.py
Normal file
3
apps/front/models.py
Normal file
@ -0,0 +1,3 @@
|
||||
from django.db import models
|
||||
|
||||
# Create your models here.
|
118
apps/front/templates/chat.html
Normal file
118
apps/front/templates/chat.html
Normal file
@ -0,0 +1,118 @@
|
||||
<!doctype html>
|
||||
<html lang="es">
|
||||
<head>
|
||||
<meta charset="UTF-8"/>
|
||||
<title>Chat</title>
|
||||
<!-- Spectre CSS -->
|
||||
<link rel="stylesheet" href="https://unpkg.com/spectre.css/dist/spectre.min.css">
|
||||
<link rel="stylesheet" href="https://unpkg.com/spectre.css/dist/spectre-exp.min.css">
|
||||
<link rel="stylesheet" href="https://unpkg.com/spectre.css/dist/spectre-icons.min.css">
|
||||
</head>
|
||||
<body>
|
||||
<div class="container grid-md">
|
||||
<h1 class="mt-2 text-center">Chat con Django</h1>
|
||||
<div class="columns">
|
||||
<aside class="column col-4">
|
||||
|
||||
<!-- Configurar alias -->
|
||||
<div class="form-group">
|
||||
<label class="form-label">
|
||||
Nombre
|
||||
<input id="nombre" class="form-input" type="text" placeholder="Oveja programadora...">
|
||||
</label>
|
||||
</div>
|
||||
<!-- Fin Configurar alias -->
|
||||
|
||||
</aside>
|
||||
<main class="column">
|
||||
|
||||
<!-- Mensajes -->
|
||||
<section id="mensajes"></section>
|
||||
<!-- Fin Mensajes -->
|
||||
|
||||
<!-- Enviar mensajes -->
|
||||
<section class="form-group">
|
||||
<input id="texto" class="form-input" type="text" placeholder="Nuevo mensaje...">
|
||||
<button id="enviar" class="btn btn-block">Enviar</button>
|
||||
</section>
|
||||
<!-- Fin Enviar mensajes -->
|
||||
</main>
|
||||
</div>
|
||||
</div>
|
||||
<script>
|
||||
/*
|
||||
* VARIABLES
|
||||
*/
|
||||
const SALA_CHAT = 'python';
|
||||
const CHAT_SOCKET = new WebSocket('ws://localhost:8000/ws/chat/' + SALA_CHAT);
|
||||
const CAMPO_NOMBRE = document.querySelector('#nombre');
|
||||
const MENSAJES = document.querySelector('#mensajes');
|
||||
const CAMPO_TEXTO = document.querySelector('#texto');
|
||||
const BOTON_ENVIAR = document.querySelector('#enviar');
|
||||
|
||||
/*
|
||||
* FUNCIONES
|
||||
*/
|
||||
function anyadirNuevoMensajeAlHTML(nombre, texto, propio = false) {
|
||||
// Contenedor
|
||||
const MI_CONTENEDOR = document.createElement('div');
|
||||
MI_CONTENEDOR.classList.add(propio ? 'bg-primary' : 'bg-secondary', 'p-2');
|
||||
// Nombre
|
||||
const MI_NOMBRE = document.createElement('h2');
|
||||
MI_NOMBRE.classList.add('text-tiny', 'text-bold', 'mt-2');
|
||||
MI_NOMBRE.textContent = nombre;
|
||||
MI_CONTENEDOR.appendChild(MI_NOMBRE);
|
||||
// Texto
|
||||
const MI_TEXTO = document.createElement('p');
|
||||
MI_TEXTO.classList.add('my-2');
|
||||
MI_TEXTO.textContent = texto;
|
||||
MI_CONTENEDOR.appendChild(MI_TEXTO);
|
||||
// Anyade todo a MENSAJES
|
||||
MENSAJES.appendChild(MI_CONTENEDOR);
|
||||
}
|
||||
|
||||
function enviarNuevoMessage() {
|
||||
// Envia al WebSocket un nuevo mensaje
|
||||
CHAT_SOCKET.send(JSON.stringify({
|
||||
nombre: CAMPO_NOMBRE.value,
|
||||
texto: CAMPO_TEXTO.value,
|
||||
fechaCreacion: Date.now()
|
||||
}));
|
||||
// Añadimos el nuevo mensaje al HTML
|
||||
anyadirNuevoMensajeAlHTML(CAMPO_NOMBRE.value, CAMPO_TEXTO.value, true);
|
||||
// Limpiamos el textarea
|
||||
CAMPO_TEXTO.value = '';
|
||||
// Le volvemos a dar el foco para escribir otro mensaje
|
||||
CAMPO_TEXTO.focus();
|
||||
}
|
||||
|
||||
/*
|
||||
* EVENTOS
|
||||
*/
|
||||
|
||||
// Conectado
|
||||
CHAT_SOCKET.addEventListener('open', () => {
|
||||
console.log('Conectado');
|
||||
});
|
||||
// Desconectado
|
||||
CHAT_SOCKET.addEventListener('close', () => {
|
||||
console.log('Desconectado');
|
||||
});
|
||||
|
||||
// Recibir mensaje
|
||||
CHAT_SOCKET.addEventListener('message', (event) => {
|
||||
console.log('Recibido nuevo mensaje');
|
||||
const miNuevaData = JSON.parse(event.data);
|
||||
// Add message to View
|
||||
this.addMessage(
|
||||
myData.member_send === this.$store.state.info_user.id,
|
||||
myData.text
|
||||
);
|
||||
});
|
||||
|
||||
// Enviar mensaje cuando se pulsa en Enviar
|
||||
BOTON_ENVIAR.addEventListener('click', enviarNuevoMessage);
|
||||
|
||||
</script>
|
||||
</body>
|
||||
</html>
|
3
apps/front/tests.py
Normal file
3
apps/front/tests.py
Normal file
@ -0,0 +1,3 @@
|
||||
from django.test import TestCase
|
||||
|
||||
# Create your tests here.
|
4
apps/front/views.py
Normal file
4
apps/front/views.py
Normal file
@ -0,0 +1,4 @@
|
||||
from django.shortcuts import render
|
||||
|
||||
def chat(request):
|
||||
return render(request, 'chat.html')
|
Reference in New Issue
Block a user