diff --git a/srcs/requirements/nestjs/api_back/src/chat/chat.controller.ts b/srcs/requirements/nestjs/api_back/src/chat/chat.controller.ts index b097ec93..b4b0c678 100644 --- a/srcs/requirements/nestjs/api_back/src/chat/chat.controller.ts +++ b/srcs/requirements/nestjs/api_back/src/chat/chat.controller.ts @@ -122,7 +122,7 @@ export class ChatController { let message = `${username} is now admin`; await this.chatService.addMessageToRoom(current_room_name, "SERVER", message); - let socket: socketDto = this.chatService.sockets.get(req.user.username); + let socket: socketDto = this.chatService.getSocket(req.user.username); let server = this.chatGateway.server; await server.in(socket.room).emit('message', "SERVER", message); @@ -207,7 +207,7 @@ export class ChatController { let message = `${req.user.username} changed the password`; room.allowed_users = [req.user.username]; await this.chatService.setPassword(req.user.username, message, room); - let socket: socketDto = this.chatService.sockets.get(req.user.username); + let socket: socketDto = this.chatService.getSocket(req.user.username); await socket.to(socket.room).emit('message', "SERVER", message); } @@ -267,7 +267,7 @@ export class ChatController { room = await this.chatService.addUserToRoom(req.user.username, room.name); } - let socket: socketDto = this.chatService.sockets.get(req.user.username); + let socket: socketDto = this.chatService.getSocket(req.user.username); await this.chatService.socketJoinRoom(socket, room.name); const ret_room = this.format_room(room); @@ -308,7 +308,7 @@ export class ChatController { } await this.chatService.setCurrentRoom(req.user.username, room.name); - let socket: socketDto = this.chatService.sockets.get(req.user.username); + let socket: socketDto = this.chatService.getSocket(req.user.username); await this.chatService.socketChangeRoom(socket, room.name); const ret_room = this.format_room(room); @@ -354,7 +354,7 @@ export class ChatController { await this.chatService.setPassword(req.user.username, message, room, old_password); // inform other connected users - let socket: socketDto = this.chatService.sockets.get(req.user.username); + let socket: socketDto = this.chatService.getSocket(req.user.username); await socket.to(socket.room).emit('message', "SERVER", message); await socket.to(socket.room).emit('new_password'); @@ -376,7 +376,7 @@ export class ChatController { await this.chatService.setPassword(req.user.username, message, room); // inform other connected users - let socket: socketDto = this.chatService.sockets.get(req.user.username); + let socket: socketDto = this.chatService.getSocket(req.user.username); await socket.to(socket.room).emit('message', "SERVER", message); await socket.to(socket.room).emit('new_password'); @@ -398,7 +398,7 @@ export class ChatController { await this.chatService.setPassword(req.user.username, message, room); // inform other connected users - let socket: socketDto = this.chatService.sockets.get(req.user.username); + let socket: socketDto = this.chatService.getSocket(req.user.username); await socket.to(socket.room).emit('message', "SERVER", message); const ret_room = this.format_room(room); @@ -445,7 +445,7 @@ export class ChatController { await this.chatService.setCurrentRoom(req.user.username, ""); // leaving message - let socket: socketDto = this.chatService.sockets.get(req.user.username); + let socket: socketDto = this.chatService.getSocket(req.user.username); await messages.forEach(async (message) => { await this.chatService.addMessageToRoom(room_name, "SERVER", message); @@ -552,7 +552,7 @@ export class ChatController { // inform other connected users let message = `${req.user.username} has muted ${mute.name} untill ${time}`; - let socket: socketDto = this.chatService.sockets.get(req.user.username); + let socket: socketDto = this.chatService.getSocket(req.user.username); let server = this.chatGateway.server; await this.chatService.addMessageToRoom(room_name, "SERVER", message); await server.in(socket.room).emit('message', "SERVER", message); @@ -601,7 +601,7 @@ export class ChatController { await this.chatService.removeMute(username, room_name); let message = `${req.user.username} has un-muted ${username}`; - let socket: socketDto = this.chatService.sockets.get(req.user.username); + let socket: socketDto = this.chatService.getSocket(req.user.username); let server = this.chatGateway.server; await this.chatService.addMessageToRoom(room_name, "SERVER", message); await server.in(socket.room).emit('message', "SERVER", message); @@ -619,7 +619,7 @@ export class ChatController { await this.chatService.addBlockUser(req.user.username, username); - let user_socket: socketDto = this.chatService.sockets.get(req.user.username); + let user_socket: socketDto = this.chatService.getSocket(req.user.username); user_socket.join(`${username}_not_emit`); res.status(HttpStatus.OK).json({ message: "successfull block" }); @@ -634,7 +634,7 @@ export class ChatController { printCaller("- in "); await this.chatService.removeBlockUser(req.user.username, username); - let user_socket: socketDto = this.chatService.sockets.get(req.user.username); + let user_socket: socketDto = this.chatService.getSocket(req.user.username); user_socket.leave(`${username}_not_emit`); res.status(HttpStatus.OK).json({ message: "successfull unblock" }); diff --git a/srcs/requirements/nestjs/api_back/src/chat/chat.gateway.ts b/srcs/requirements/nestjs/api_back/src/chat/chat.gateway.ts index 60745c84..c42696bd 100644 --- a/srcs/requirements/nestjs/api_back/src/chat/chat.gateway.ts +++ b/srcs/requirements/nestjs/api_back/src/chat/chat.gateway.ts @@ -31,7 +31,7 @@ implements OnGatewayConnection, OnGatewayDisconnect if (!socket.username) return; - this.chatService.sockets.set(socket.username, socket); + this.chatService.addSocket(socket.username, socket); printCaller("--- socket.username:", socket.username); let not_emit: string = `${socket.username}_not_emit`; @@ -47,7 +47,7 @@ implements OnGatewayConnection, OnGatewayDisconnect socket.join(current_room); } async handleDisconnect(socket: socketDto) { - this.chatService.sockets.delete(socket.username); + this.chatService.removeSocket(socket.username); } @SubscribeMessage('join') diff --git a/srcs/requirements/nestjs/api_back/src/chat/chat.service.ts b/srcs/requirements/nestjs/api_back/src/chat/chat.service.ts index 60dca96f..e761dfd8 100644 --- a/srcs/requirements/nestjs/api_back/src/chat/chat.service.ts +++ b/srcs/requirements/nestjs/api_back/src/chat/chat.service.ts @@ -32,6 +32,28 @@ export class ChatService { sockets = new Map(); + addSocket(key: string, socket: socketDto) + { + printCaller(`-- in (key: ${key}) `); + this.sockets.set(key, socket); + console.log("this.sockets:", this.sockets); + } + + getSocket(key: string) + { + printCaller("-- in "); + let socket = this.sockets.get(key); + console.log("this.sockets:", this.sockets); + console.log("socket:", socket); + return socket; + } + + removeSocket(key: string) + { + printCaller("-- in "); + this.sockets.delete(key); + } + // temp for test sleep(ms) { return new Promise(resolve => setTimeout(resolve, ms)); @@ -181,7 +203,7 @@ export class ChatService { const user_db = await this.getUserByName(username); if (!user_db) { - printCaller(`error: 'the user was not found'`); + printCaller(`error: 'the user ${username} was not found'`); return; } @@ -689,8 +711,13 @@ export class ChatService { { printCaller("-- in "); - let room_name = await this.getCurrentRoomName(socket.username); + let room_name = await this.getCurrentRoomName(socket.username); + if (!room_name) + { + console.log(`couldn't find room by username: ${socket.username}`); + return; + } const current_room = await this.getRoomByName(room_name); if (current_room.protection) @@ -768,6 +795,7 @@ export class ChatService { console.log("old_name:", old_name); console.log("new_name:", new_name); + console.log("sockets:", this.sockets); let rooms: Chatroom[] = await this.getAllRooms(); await rooms.forEach((room) => @@ -816,13 +844,19 @@ export class ChatService { this.addMessageToRoom(room.name, "SERVER", `${old_name} changes it's name for ${new_name}`); - let socket: socketDto = this.sockets.get(old_name); + //let socket: socketDto = this.sockets.get(old_name); + //let socket = this.sockets.get(old_name); + let socket = this.getSocket(old_name); + console.log("sockets:", this.sockets); if (socket) { + console.log("=== found socket"); this.sockets.delete(old_name); socket.username = new_name; this.sockets.set(new_name, socket); } + else + console.log("=== socket not found"); });