Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[BE/#475] 채팅 강제 disconnect 처리 #480

Merged
merged 3 commits into from
Dec 11, 2023
Merged
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 10 additions & 0 deletions BE/src/chat/chats.gateway.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ interface ChatWebSocket extends WebSocket {
})
export class ChatsGateway implements OnGatewayConnection, OnGatewayDisconnect {
@WebSocketServer() server: Server;
private users = new Map<string, ChatWebSocket>();
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

유저들을 따로 관리 하면 좀 더 효율적으로 관리가 가능 하겠네요

private rooms = new Map<number, Set<ChatWebSocket>>();
private readonly logger = new Logger('ChatsGateway');
constructor(private readonly chatService: ChatService) {}
Expand All @@ -39,10 +40,12 @@ export class ChatsGateway implements OnGatewayConnection, OnGatewayDisconnect {
const roomId = client.roomId;
const room = this.rooms.get(roomId);
room.delete(client);

if (room.size === 0) {
this.rooms.delete(roomId);
}
}

this.logger.debug(
`[${client.userId}] on disconnect => left rooms : ${Array.from(
this.rooms.keys(),
Expand Down Expand Up @@ -91,8 +94,14 @@ export class ChatsGateway implements OnGatewayConnection, OnGatewayDisconnect {
@MessageBody() message: object,
@ConnectedSocket() client: ChatWebSocket,
) {
if (this.users.has(client.userId)) {
// 이전에 접속한 방이 아직 남아있으면
this.users.get(client.userId).close();
}
this.users.set(client.userId, client);
const roomId = message['room_id'];
client.roomId = roomId;
console.log(client.roomId);
if (this.rooms.has(roomId)) {
this.rooms.get(roomId).add(client);

Expand All @@ -108,6 +117,7 @@ export class ChatsGateway implements OnGatewayConnection, OnGatewayDisconnect {
);
});
} else this.rooms.set(roomId, new Set([client]));

this.logger.debug(
`[${client.userId}] join room : ${roomId}`,
'ChatsGateway',
Expand Down
Loading