Skip to content

Commit be28078

Browse files
committed
Initial commit: Pizza Delivery con Relay Gateway
0 parents  commit be28078

5 files changed

Lines changed: 771 additions & 0 deletions

File tree

.github/workflows/deploy.yml

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
name: Deploy to GitHub Pages
2+
3+
on:
4+
push:
5+
branches:
6+
- master
7+
workflow_dispatch:
8+
9+
permissions:
10+
contents: read
11+
pages: write
12+
id-token: write
13+
14+
concurrency:
15+
group: "pages"
16+
cancel-in-progress: false
17+
18+
jobs:
19+
build:
20+
runs-on: ubuntu-latest
21+
steps:
22+
- name: Checkout
23+
uses: actions/checkout@v4
24+
25+
- name: Update Relay URL for production
26+
run: |
27+
sed -i.bak "s|const RELAY_URL = 'http://localhost:5000';|const RELAY_URL = window.location.hostname === 'coderic.org' ? 'wss://demo.relay.coderic.net' : 'http://localhost:5000';|g" index.html
28+
rm -f index.html.bak
29+
30+
- name: Setup Pages
31+
uses: actions/configure-pages@v4
32+
33+
- name: Upload artifact
34+
uses: actions/upload-pages-artifact@v3
35+
with:
36+
path: .
37+
38+
deploy:
39+
environment:
40+
name: github-pages
41+
url: ${{ steps.deployment.outputs.page_url }}
42+
runs-on: ubuntu-latest
43+
needs: build
44+
steps:
45+
- name: Deploy to GitHub Pages
46+
id: deployment
47+
uses: actions/deploy-pages@v4
48+

README.md

Lines changed: 97 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,97 @@
1+
# 🍕 Pizza Delivery - Tracking en Tiempo Real
2+
3+
Ejemplo de sistema de pedidos de pizza con tracking en tiempo real utilizando [Relay Gateway](https://github.com/NeftaliYagua/Relay).
4+
5+
![Demo](https://img.shields.io/badge/demo-online-green)
6+
7+
## 🚀 Inicio Rápido
8+
9+
### Prerrequisitos
10+
11+
1. Tener Relay Gateway ejecutándose en `http://localhost:5000`
12+
13+
```bash
14+
# Opción 1: Con Docker Compose (recomendado)
15+
cd infraestructura && docker compose up -d
16+
17+
# Opción 2: Directo con npx
18+
npx relay-gateway
19+
```
20+
21+
### Ejecutar el ejemplo
22+
23+
```bash
24+
# Clonar este repositorio
25+
git clone https://github.com/Coderic/relay-ejemplo-pizza-delivery.git
26+
cd relay-ejemplo-pizza-delivery
27+
28+
# Servir los archivos estáticos
29+
npx serve -p 8001
30+
```
31+
32+
Abre http://localhost:8001 en tu navegador.
33+
34+
## 📖 Características
35+
36+
- **Vista Cliente**: Selecciona pizzas y realiza pedidos
37+
- **Vista Cocina**: Gestiona los pedidos y actualiza estados
38+
- **Tracking en tiempo real**: Observa el progreso de tu pedido
39+
40+
### Estados del pedido
41+
42+
1. 📝 **Recibido** - Pedido registrado
43+
2. 👨‍🍳 **Preparando** - En la cocina
44+
3. 🔥 **Horneando** - En el horno
45+
4.**Listo** - Esperando repartidor
46+
5. 🛵 **En Camino** - El repartidor va hacia ti
47+
6. 🎉 **Entregado** - ¡Buen provecho!
48+
49+
## 💻 Cómo funciona
50+
51+
```javascript
52+
// Conectar a Relay
53+
const relay = new RelayConector('http://localhost:5000');
54+
await relay.conectar();
55+
56+
// Enviar nuevo pedido (cliente)
57+
relay.enviarATodos({
58+
tipo: 'nuevo_pedido',
59+
pedidoId: 'ABC123',
60+
pizza: 'Pepperoni',
61+
precio: 14.99
62+
});
63+
64+
// Actualizar estado (cocina)
65+
relay.enviarATodos({
66+
tipo: 'estado_pedido',
67+
pedidoId: 'ABC123',
68+
estado: 'preparando'
69+
});
70+
71+
// Escuchar actualizaciones
72+
relay.on('relay', (data) => {
73+
if (data.tipo === 'estado_pedido') {
74+
actualizarTracking(data.estado);
75+
}
76+
});
77+
```
78+
79+
## 📁 Estructura
80+
81+
```
82+
├── index.html # Interfaz cliente/cocina
83+
├── conector.js # Cliente Relay para navegador
84+
├── package.json
85+
└── README.md
86+
```
87+
88+
## 🔗 Enlaces
89+
90+
- [Relay Gateway](https://github.com/NeftaliYagua/Relay)
91+
- [Documentación](https://neftaliyagua.github.io/Relay/)
92+
- [Otros ejemplos](https://github.com/Coderic?q=relay-ejemplo)
93+
94+
## 📄 Licencia
95+
96+
MIT © [Coderic](https://github.com/Coderic)
97+

conector.js

Lines changed: 127 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,127 @@
1+
/**
2+
* Conector Relay v2
3+
* Cliente JavaScript para conectarse al gateway Relay
4+
*
5+
* Uso:
6+
* const relay = new RelayConector('http://localhost:5000');
7+
* await relay.conectar();
8+
* await relay.identificar('miUsuario');
9+
* relay.enviarATodos({ mensaje: 'Hola!' });
10+
* relay.on('relay', (data) => console.log(data));
11+
*/
12+
13+
class RelayConector {
14+
constructor(url = 'http://localhost:5000') {
15+
this.url = url;
16+
this.socket = null;
17+
this.usuario = null;
18+
this.listeners = {};
19+
this.connected = false;
20+
}
21+
22+
conectar() {
23+
return new Promise((resolve, reject) => {
24+
if (typeof io === 'undefined') {
25+
reject(new Error('Socket.io no cargado. Incluye: <script src="https://cdn.socket.io/4.7.4/socket.io.min.js"></script>'));
26+
return;
27+
}
28+
29+
this.socket = io(this.url + '/relay', {
30+
transports: ['websocket', 'polling']
31+
});
32+
33+
this.socket.on('connect', () => {
34+
console.log('Relay: Conectado -', this.socket.id);
35+
this.connected = true;
36+
this._emit('connect', { socketId: this.socket.id });
37+
resolve(this);
38+
});
39+
40+
this.socket.on('disconnect', (reason) => {
41+
console.log('Relay: Desconectado -', reason);
42+
this.connected = false;
43+
this._emit('disconnect', { reason });
44+
});
45+
46+
this.socket.on('connect_error', (error) => {
47+
console.error('Relay: Error -', error.message);
48+
this._emit('error', error);
49+
reject(error);
50+
});
51+
52+
// Eventos de Relay
53+
this.socket.on('notificar', (data) => this._emit('notificar', data));
54+
this.socket.on('relay', (data) => this._emit('relay', data));
55+
});
56+
}
57+
58+
identificar(usuario) {
59+
return new Promise((resolve) => {
60+
this.usuario = usuario;
61+
this.socket.emit('identificar', usuario, (ok) => {
62+
console.log('Relay: Identificado como', usuario);
63+
this._emit('identificado', { usuario, ok });
64+
resolve(ok);
65+
});
66+
});
67+
}
68+
69+
// Enviar mensaje por el canal 'relay'
70+
enviar(data) {
71+
this.socket.emit('relay', data);
72+
}
73+
74+
// Enviar notificación
75+
notificar(data) {
76+
this.socket.emit('notificar', data);
77+
}
78+
79+
// Atajos para destinos
80+
enviarAMi(data) {
81+
this.enviar({ ...data, destino: 'yo' });
82+
}
83+
84+
enviarAOtros(data) {
85+
this.enviar({ ...data, destino: 'ustedes' });
86+
}
87+
88+
enviarATodos(data) {
89+
this.enviar({ ...data, destino: 'nosotros' });
90+
}
91+
92+
// Sistema de eventos
93+
on(evento, callback) {
94+
if (!this.listeners[evento]) {
95+
this.listeners[evento] = [];
96+
}
97+
this.listeners[evento].push(callback);
98+
return this;
99+
}
100+
101+
off(evento, callback) {
102+
if (this.listeners[evento]) {
103+
this.listeners[evento] = this.listeners[evento].filter(cb => cb !== callback);
104+
}
105+
return this;
106+
}
107+
108+
_emit(evento, data) {
109+
if (this.listeners[evento]) {
110+
this.listeners[evento].forEach(cb => cb(data));
111+
}
112+
}
113+
114+
desconectar() {
115+
if (this.socket) {
116+
this.socket.disconnect();
117+
}
118+
}
119+
}
120+
121+
// Exportar para uso en módulos o global
122+
if (typeof module !== 'undefined' && module.exports) {
123+
module.exports = RelayConector;
124+
} else {
125+
window.RelayConector = RelayConector;
126+
}
127+

0 commit comments

Comments
 (0)