Skip to content

Commit

Permalink
[작업중] 유저리스트 중복 삭제
Browse files Browse the repository at this point in the history
  • Loading branch information
HeeDragoN1123 committed Nov 9, 2023
1 parent f967398 commit 8231f11
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 254 deletions.
42 changes: 23 additions & 19 deletions src/chats/chats.gateway.ts
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,6 @@ export class ChatsGateway
socket.broadcast.emit('disconnect_user', user);
await user.deleteOne();

// userList에서 해당 유저 정보 제거
this.userList = this.userList.filter(
(u) => u.userId !== user.data.userId
);
Expand All @@ -76,7 +75,6 @@ export class ChatsGateway
this.logger.log(`connected : ${socket.id} ${socket.nsp.name}`);
// await this.logger.log(`connected : ${socket.id} ${socket.nsp.name}`);
}
// 클라이언트가 'join_room' 메시지를 보낼 때 실행되는 메소드
@SubscribeMessage('join_room')
async handleJoinRoom(
@MessageBody()
Expand All @@ -88,27 +86,33 @@ export class ChatsGateway
},
@ConnectedSocket() socket: Socket
) {
socket.join(String(payload.roomId));
this.logger.log(
`Joined room: ${payload.roomId}, Nickname: ${payload.nickname}`
const isUserAlreadyInList = this.userList.some(
(user) => user.userId === payload.userId
);

//이전 채팅 내용을 불러옵니다.
const chatHistory = await this.getChatHistory(payload.roomId);
if (!isUserAlreadyInList) {
socket.join(String(payload.roomId));
this.logger.log(
`Joined room: ${payload.roomId}, Nickname: ${payload.nickname}`
);

// userList에 사용자 정보 추가
this.userList.push({
nickname: payload.nickname,
profileImg: payload.profileImg,
userId: payload.userId,
});
// 이전 채팅 내용을 불러옵니다.
const chatHistory = await this.getChatHistory(payload.roomId);

// 이전 채팅 내용과 함께 사용자 정보를 클라이언트에게 전송합니다.
socket.emit('chat_history', chatHistory);
// 방에 있는 모든 사용자에게 userList 전송
this.server.to(String(payload.roomId)).emit('user_connected', payload);
console.log(this.userList);
this.server.to(String(payload.roomId)).emit('userList', this.userList);
// userList에 사용자 정보 추가
this.userList.push({
nickname: payload.nickname,
profileImg: payload.profileImg,
userId: payload.userId,
});

// 이전 채팅 내용과 함께 사용자 정보를 클라이언트에게 전송합니다.
socket.emit('chat_history', chatHistory);
// 방에 있는 모든 사용자에게 userList 전송
this.server.to(String(payload.roomId)).emit('user_connected', payload);
console.log('유저리스트 콘솔', this.userList);
this.server.to(String(payload.roomId)).emit('userList', this.userList);
}
}

// 방에서 적었던 채팅 내용을 불러오는 매서드
Expand Down
4 changes: 1 addition & 3 deletions src/chats/chats.module.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,15 +3,13 @@ import { Module } from '@nestjs/common';
import { ChatsGateway } from './chats.gateway';
import { MongooseModule } from '@nestjs/mongoose';
import { Chatting, ChattingSchema } from './models/chattings.model';
import { ChatsService } from './chats.service';

@Module({
imports: [
MongooseModule.forFeature([
{ name: Chatting.name, schema: ChattingSchema },
{ name: SocketModel.name, schema: SocketSchema },
]),
],
providers: [ChatsGateway, ChatsService],
providers: [ChatsGateway],
})
export class ChatsModule {}
232 changes: 0 additions & 232 deletions src/chats/chats.service.ts

This file was deleted.

0 comments on commit 8231f11

Please sign in to comment.