-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
5 changed files
with
113 additions
and
4 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
import { Usuario } from './usuario'; | ||
|
||
export class UsuarioLista{ | ||
|
||
private lista:Usuario[]=[]; | ||
|
||
constructor(){ | ||
|
||
} | ||
// Agregar un usuario | ||
public agregar(usuario:Usuario){ | ||
this.lista.push(usuario); | ||
console.log(this.lista); | ||
return usuario; | ||
} | ||
|
||
public actualizarNombre(id:string,nombre:string){ | ||
for(let usuario of this.lista) | ||
{ | ||
if(usuario.id===id){ | ||
usuario.nombre=nombre; | ||
break; | ||
} | ||
} | ||
|
||
console.log('======= Actualizando Usuario ========='); | ||
console.log(this.lista); | ||
} | ||
// obtener lista | ||
|
||
public getLista(){ | ||
return this.lista; | ||
} | ||
|
||
public getUsaurio(id:string){return this.lista.find(usuario=> usuario.id=== id)} | ||
|
||
public getUsuarioEnSala(sala:string){return this.lista.filter(usuario=>usuario.sala===sala)} | ||
|
||
// Borrar Usuario | ||
|
||
public borrarUsuarios(id:string){ | ||
const tempUsuario=this.getUsaurio(id); | ||
this.lista= this.lista.filter(usuario=>usuario.id !== id); | ||
//console.log(this.lista); | ||
return tempUsuario; | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
|
||
export class Usuario { | ||
|
||
public id: string; | ||
public nombre:string; | ||
public sala:string; | ||
constructor(id:string){ | ||
|
||
this.id=id; | ||
this.nombre='sin-nombre'; | ||
this.sala='sin-sala'; | ||
} | ||
|
||
|
||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,24 +1,49 @@ | ||
import { Socket } from 'socket.io'; | ||
import Server from '../classes/server'; | ||
import socketIO from 'socket.io'; | ||
import { UsuarioLista } from '../classes/usuario-lista'; | ||
import { Usuario } from '../classes/usuario'; | ||
|
||
|
||
export const usuariosConectados= new UsuarioLista(); | ||
|
||
export const conectarCliente=(cliente:Socket)=>{ | ||
const usuario = new Usuario(cliente.id); | ||
usuariosConectados.agregar(usuario); | ||
} | ||
|
||
export const desconectar = (cliente: Socket) => { | ||
cliente.on('disconnect' ,()=>{ | ||
console.log('Cliente desconectado'); | ||
usuariosConectados.borrarUsuarios(cliente.id); | ||
}); | ||
} | ||
|
||
|
||
|
||
// Escuchar mensaje | ||
export const mensaje =(Cliente:Socket,io:SocketIO.Server)=>{ | ||
export const mensaje =(cliente:Socket,io:SocketIO.Server)=>{ | ||
|
||
Cliente.on('mensaje',(payload:{de:string,cuerpo:string})=>{ | ||
cliente.on('mensaje',(payload:{de:string,cuerpo:string})=>{ | ||
console.log('Mensaje recibido',payload); | ||
io.emit('mensaje-nuevo',payload); | ||
}); | ||
|
||
} | ||
|
||
//configurar usuario | ||
export const configurarUsuario =(cliente:Socket,io:SocketIO.Server)=>{ | ||
|
||
cliente.on('configurar-usuario',(payload:{nombre:string},callback:Function)=>{ | ||
// console.log('Configurando Usuario',payload.nombre); | ||
|
||
usuariosConectados.actualizarNombre(cliente.id,payload.nombre) | ||
callback({ | ||
ok:true, | ||
mensaje:`Usuario ${payload.nombre}, cofigurado` | ||
}) | ||
|
||
// io.emit('mensaje-nuevo',payload); | ||
}); | ||
|
||
} |