first commit
This commit is contained in:
61
src/sala-chat/sala-chat.gateway.ts
Normal file
61
src/sala-chat/sala-chat.gateway.ts
Normal file
@@ -0,0 +1,61 @@
|
||||
import {
|
||||
OnGatewayConnection,
|
||||
OnGatewayDisconnect,
|
||||
SubscribeMessage,
|
||||
WebSocketGateway,
|
||||
WebSocketServer,
|
||||
} from '@nestjs/websockets';
|
||||
import { SalaChatService } from './sala-chat.service';
|
||||
import { Server, Socket } from 'socket.io';
|
||||
|
||||
@WebSocketGateway({ namespace: 'salachat' })
|
||||
export class SalaChatGateway
|
||||
implements OnGatewayConnection, OnGatewayDisconnect
|
||||
{
|
||||
@WebSocketServer()
|
||||
server: Server;
|
||||
constructor(private readonly salaChatService: SalaChatService) {}
|
||||
|
||||
handleConnection(client: Socket) {
|
||||
const userId = client.handshake.auth.userId;
|
||||
const userConectado = this.salaChatService.conectaUsuarioUUID(userId);
|
||||
if (userConectado){
|
||||
client.join('chat_general');
|
||||
client.emit('onConnectRoom',this.salaChatService.listaUsuariosSinPartidas);
|
||||
client.broadcast.to('chat_general').emit('onUserConnectRoom',userConectado);
|
||||
}else{
|
||||
client.disconnect();
|
||||
}
|
||||
}
|
||||
|
||||
handleDisconnect(client: Socket) {
|
||||
const userId = client.handshake.auth.userId;
|
||||
const userDesconectado = this.salaChatService.desconectaUsuarioUUID(userId);
|
||||
if (userDesconectado){
|
||||
client.broadcast.to('chat_general').emit('onUserDisconnectRoom',userDesconectado);
|
||||
}
|
||||
client.leave('chat_general');
|
||||
}
|
||||
|
||||
@SubscribeMessage('chatMsg')
|
||||
handleMsg(client: Socket, msg: string) {
|
||||
const userId = client.handshake.auth.userId;
|
||||
const user = this.salaChatService.getUsuarioUUID(userId);
|
||||
if (user) {
|
||||
this.server.to('chat_general').emit('chatMsg', { uuid:userId, msg });
|
||||
} else {
|
||||
client.disconnect();
|
||||
}
|
||||
}
|
||||
|
||||
sendBroadcastMsg(msg:string){
|
||||
this.server.to('chat_general').emit('broadcastMsg',msg);
|
||||
}
|
||||
|
||||
@SubscribeMessage('creaPartida')
|
||||
handleCreaPartida(client: Socket) {
|
||||
const userId = client.handshake.auth.userId;
|
||||
const partida= this.salaChatService.creaPartida(userId);
|
||||
this.server.to('chat_general').emit('on_create_partida',partida);
|
||||
}
|
||||
}
|
||||
11
src/sala-chat/sala-chat.module.ts
Normal file
11
src/sala-chat/sala-chat.module.ts
Normal file
@@ -0,0 +1,11 @@
|
||||
import { Module } from '@nestjs/common';
|
||||
import { SalaChatService } from './sala-chat.service';
|
||||
import { SalaChatGateway } from './sala-chat.gateway';
|
||||
import { SharedModule } from 'src/shared/shared.module';
|
||||
|
||||
|
||||
@Module({
|
||||
imports:[SharedModule],
|
||||
providers: [SalaChatGateway, SalaChatService],
|
||||
})
|
||||
export class SalaChatModule {}
|
||||
58
src/sala-chat/sala-chat.service.ts
Normal file
58
src/sala-chat/sala-chat.service.ts
Normal file
@@ -0,0 +1,58 @@
|
||||
import { Injectable } from '@nestjs/common';
|
||||
import { Partida, PartidasService } from 'src/shared/partidas.service';
|
||||
import { UsuariosService } from 'src/shared/usuarios.service';
|
||||
|
||||
@Injectable()
|
||||
export class SalaChatService {
|
||||
constructor(
|
||||
private readonly usuariosService: UsuariosService,
|
||||
private readonly partidasService: PartidasService,
|
||||
) {}
|
||||
|
||||
get listaUsuariosSinPartidas() {
|
||||
return Array.from(this.usuariosService.usuarios.values()).map(
|
||||
({ uuid, nickname, conectado }) => ({
|
||||
uuid,
|
||||
nickname,
|
||||
conectado,
|
||||
}),
|
||||
);
|
||||
}
|
||||
|
||||
getUsuarioUUID(uuid: string) {
|
||||
return this.usuariosService.getUsuarioByUUID(uuid);
|
||||
}
|
||||
conectaUsuarioUUID(uuid: string) {
|
||||
const user = this.usuariosService.getUsuarioByUUID(uuid);
|
||||
if (user) {
|
||||
user.conectado = true;
|
||||
return {
|
||||
uuid: user.uuid,
|
||||
nickname: user.nickname,
|
||||
conectado: user.conectado,
|
||||
};
|
||||
} else {
|
||||
return undefined;
|
||||
}
|
||||
}
|
||||
|
||||
desconectaUsuarioUUID(uuid: string) {
|
||||
const user = this.usuariosService.getUsuarioByUUID(uuid);
|
||||
if (user) {
|
||||
user.conectado = false;
|
||||
return {
|
||||
uuid: user.uuid,
|
||||
nickname: user.nickname,
|
||||
conectado: user.conectado,
|
||||
};
|
||||
} else {
|
||||
return undefined;
|
||||
}
|
||||
}
|
||||
|
||||
creaPartida(uuidJugadorCreador: string) {
|
||||
const partida = this.partidasService.creaPartida(uuidJugadorCreador);
|
||||
this.usuariosService.addPartidaToUsuario(uuidJugadorCreador, partida);
|
||||
return partida;
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user