-
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathchat-basico.html
More file actions
327 lines (276 loc) · 9.65 KB
/
chat-basico.html
File metadata and controls
327 lines (276 loc) · 9.65 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
<!DOCTYPE html>
<html lang="es">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Chat Básico - Relay v2.2</title>
<style>
@import url('https://fonts.googleapis.com/css2?family=Space+Grotesk:wght@400;500;700&display=swap');
* { margin: 0; padding: 0; box-sizing: border-box; }
body {
font-family: 'Space Grotesk', sans-serif;
background: linear-gradient(135deg, #1a1a2e 0%, #16213e 50%, #0f3460 100%);
min-height: 100vh;
display: flex;
justify-content: center;
align-items: center;
padding: 1rem;
}
.chat-container {
width: 100%;
max-width: 500px;
background: rgba(255, 255, 255, 0.03);
border: 1px solid rgba(255, 255, 255, 0.1);
border-radius: 24px;
overflow: hidden;
box-shadow: 0 25px 50px rgba(0, 0, 0, 0.5);
}
.chat-header {
background: linear-gradient(135deg, #e94560, #ff6b9d);
padding: 1.5rem;
text-align: center;
}
.chat-header h1 { color: white; font-size: 1.5rem; }
.status {
display: flex; align-items: center; justify-content: center;
gap: 0.5rem; margin-top: 0.5rem; font-size: 0.85rem; color: rgba(255,255,255,0.8);
}
.status-dot {
width: 8px; height: 8px; border-radius: 50%; background: #ff4444;
}
.status-dot.online { background: #00ff88; box-shadow: 0 0 10px #00ff88; }
.users-bar {
padding: 0.75rem 1.5rem;
background: rgba(0,0,0,0.2);
border-bottom: 1px solid rgba(255,255,255,0.05);
font-size: 0.85rem; color: #aaa;
}
.chat-messages {
height: 350px;
overflow-y: auto;
padding: 1rem;
display: flex;
flex-direction: column;
gap: 0.5rem;
}
.message {
max-width: 80%;
padding: 0.75rem 1rem;
border-radius: 16px;
animation: fadeIn 0.3s ease;
}
@keyframes fadeIn { from { opacity: 0; transform: translateY(10px); } }
.message.received {
align-self: flex-start;
background: rgba(255,255,255,0.08);
border: 1px solid rgba(255,255,255,0.1);
color: #e0e0e0;
}
.message.sent {
align-self: flex-end;
background: linear-gradient(135deg, #e94560, #ff6b9d);
color: white;
}
.message .user { font-size: 0.7rem; opacity: 0.7; margin-bottom: 0.25rem; }
.message .text { font-size: 0.95rem; line-height: 1.4; }
.message .time { font-size: 0.65rem; opacity: 0.5; text-align: right; margin-top: 0.25rem; }
.message.system {
align-self: center;
background: transparent;
color: #888;
font-size: 0.8rem;
text-align: center;
}
.chat-input {
display: flex; gap: 0.5rem; padding: 1rem;
background: rgba(0,0,0,0.3);
border-top: 1px solid rgba(255,255,255,0.05);
}
.chat-input input {
flex: 1;
padding: 0.875rem 1.25rem;
border: 1px solid rgba(255,255,255,0.1);
border-radius: 25px;
background: rgba(255,255,255,0.05);
color: white;
font-family: inherit;
outline: none;
}
.chat-input input:focus { border-color: #e94560; }
.chat-input input::placeholder { color: rgba(255,255,255,0.3); }
.chat-input button {
width: 50px; height: 50px;
border: none; border-radius: 50%;
background: linear-gradient(135deg, #e94560, #ff6b9d);
color: white; font-size: 1.25rem;
cursor: pointer; transition: transform 0.2s;
}
.chat-input button:hover { transform: scale(1.1); }
/* Login */
.login-overlay {
position: fixed; inset: 0;
background: rgba(0,0,0,0.9);
display: flex; justify-content: center; align-items: center;
z-index: 100;
}
.login-box {
background: linear-gradient(135deg, #1a1a2e, #16213e);
border: 1px solid rgba(255,255,255,0.1);
border-radius: 24px;
padding: 2rem;
text-align: center;
width: 90%; max-width: 350px;
}
.login-box h2 { color: white; margin-bottom: 0.5rem; }
.login-box p { color: #888; margin-bottom: 1.5rem; }
.login-box input {
width: 100%; padding: 1rem;
border: 1px solid rgba(255,255,255,0.1);
border-radius: 12px;
background: rgba(255,255,255,0.05);
color: white; font-family: inherit;
margin-bottom: 1rem; outline: none;
}
.login-box button {
width: 100%; padding: 1rem;
border: none; border-radius: 12px;
background: linear-gradient(135deg, #e94560, #ff6b9d);
color: white; font-weight: 600;
cursor: pointer;
}
.hidden { display: none !important; }
</style>
</head>
<body>
<div class="login-overlay" id="login">
<div class="login-box">
<h2>💬 Chat en Vivo</h2>
<p>Ingresa tu nombre para unirte</p>
<input type="text" id="nombre" placeholder="Tu nombre..." autofocus>
<button onclick="entrar()">Entrar</button>
</div>
</div>
<div class="chat-container">
<div class="chat-header">
<h1>💬 Chat en Tiempo Real</h1>
<div class="status">
<span class="status-dot" id="dot"></span>
<span id="estado">Desconectado</span>
</div>
</div>
<div class="users-bar">Usuarios: <span id="usuarios">0</span></div>
<div class="chat-messages" id="mensajes"></div>
<div class="chat-input">
<input type="text" id="texto" placeholder="Escribe un mensaje..." disabled>
<button onclick="enviarMensaje()" id="btnEnviar" disabled>➤</button>
</div>
</div>
<script src="https://cdn.socket.io/4.7.4/socket.io.min.js"></script>
<script src="conector.js"></script>
<script>
// Configuración - URL de Relay Gateway
const RELAY_URL = 'wss://demo.relay.coderic.net';
let relay;
let miNombre = '';
let usuariosOnline = new Set();
// Enter para login
document.getElementById('nombre').addEventListener('keypress', e => {
if (e.key === 'Enter') entrar();
});
// Enter para enviar
document.getElementById('texto').addEventListener('keypress', e => {
if (e.key === 'Enter') enviarMensaje();
});
async function entrar() {
miNombre = document.getElementById('nombre').value.trim();
if (!miNombre) return;
document.getElementById('login').classList.add('hidden');
// Conectar a Relay
relay = new RelayConector(RELAY_URL);
relay.on('connect', () => {
document.getElementById('dot').classList.add('online');
document.getElementById('estado').textContent = 'Conectado';
document.getElementById('texto').disabled = false;
document.getElementById('btnEnviar').disabled = false;
});
relay.on('disconnect', () => {
document.getElementById('dot').classList.remove('online');
document.getElementById('estado').textContent = 'Desconectado';
agregarSistema('Conexión perdida');
});
// Escuchar mensajes del canal 'relay'
relay.on('relay', (data) => {
switch(data.tipo) {
case 'mensaje':
agregarMensaje(data.usuario, data.texto, data.usuario === miNombre);
break;
case 'entrada':
if (data.usuario !== miNombre) {
usuariosOnline.add(data.usuario);
actualizarUsuarios();
agregarSistema(`${data.usuario} entró al chat`);
// Responder presencia
relay.enviarAOtros({ tipo: 'presencia', usuario: miNombre });
}
break;
case 'presencia':
usuariosOnline.add(data.usuario);
actualizarUsuarios();
break;
case 'salida':
usuariosOnline.delete(data.usuario);
actualizarUsuarios();
agregarSistema(`${data.usuario} salió del chat`);
break;
}
});
await relay.conectar();
await relay.identificar(miNombre);
agregarSistema(`Bienvenido, ${miNombre}`);
// Anunciar entrada
relay.enviarATodos({ tipo: 'entrada', usuario: miNombre });
// Anunciar salida al cerrar
window.addEventListener('beforeunload', () => {
relay.enviarAOtros({ tipo: 'salida', usuario: miNombre });
});
}
function enviarMensaje() {
const texto = document.getElementById('texto').value.trim();
if (!texto) return;
relay.enviarATodos({
tipo: 'mensaje',
usuario: miNombre,
texto: texto,
hora: new Date().toISOString()
});
document.getElementById('texto').value = '';
}
function agregarMensaje(usuario, texto, esMio) {
const div = document.createElement('div');
div.className = `message ${esMio ? 'sent' : 'received'}`;
const hora = new Date().toLocaleTimeString('es', { hour: '2-digit', minute: '2-digit' });
div.innerHTML = `
${!esMio ? `<div class="user">${usuario}</div>` : ''}
<div class="text">${escapeHtml(texto)}</div>
<div class="time">${hora}</div>
`;
document.getElementById('mensajes').appendChild(div);
div.scrollIntoView({ behavior: 'smooth' });
}
function agregarSistema(texto) {
const div = document.createElement('div');
div.className = 'message system';
div.textContent = texto;
document.getElementById('mensajes').appendChild(div);
}
function actualizarUsuarios() {
document.getElementById('usuarios').textContent = usuariosOnline.size + 1;
}
function escapeHtml(text) {
const div = document.createElement('div');
div.textContent = text;
return div.innerHTML;
}
</script>
</body>
</html>