Skip to content

Commit

Permalink
Feat: 방 인원 다 찾는지 검사 / verify-room 이벤트
Browse files Browse the repository at this point in the history
  • Loading branch information
zaehuun committed Nov 18, 2021
1 parent 35f31d7 commit 7abd682
Showing 1 changed file with 24 additions and 8 deletions.
32 changes: 24 additions & 8 deletions server/socket/src/gateway/room.gateway.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,28 +31,44 @@ export class RoomGateway implements OnGatewayInit, OnGatewayConnection, OnGatewa
}

@SubscribeMessage('join-room')
handleJoinRoom(client: Socket, { field, img, nickname, roomId }: IRoomRequest): void {
handleJoinRoom(client: Socket, { field, img, nickname, roomId, maxHead }: IRoomRequest): void {
client.leave(client.id); //verify-room에서 생성한 룸을 삭제합니다. 이제 사용될 일이 없어서..
client.join(roomId);
if (!this.userList[roomId]) this.userList[roomId] = {};
this.userList[roomId][nickname] = {
if (!this.userList[roomId]) this.userList[roomId] = { list: {}, maxHead: maxHead };
this.userList[roomId]['list'][nickname] = {
field: field,
img: img,
};
this.server.to(roomId).emit('user-list', this.userList[roomId]);
this.server.to(roomId).emit('user-list', this.userList[roomId]['list']);
}

@SubscribeMessage('leave-room')
handleLeaveRoom(client: Socket, { nickname, roomId }: IRoomRequest): void {
client.leave(roomId);
delete this.userList[roomId][nickname];
this.server.to(roomId).emit('user-list', this.userList[roomId]);
delete this.userList[roomId]['list'][nickname];
this.server.to(roomId).emit('user-list', this.userList[roomId]['list']);
}

@SubscribeMessage('kick-room')
handleKickRoom(client: Socket, { roomId, kickNickname }: IRoomRequest): void {
if (!kickNickname) return;
delete this.userList[roomId][kickNickname];
this.server.to(roomId).emit('user-list', this.userList[roomId]);
delete this.userList[roomId]['list'][kickNickname];
this.server.to(roomId).emit('user-list', this.userList[roomId]['list']);
}

@SubscribeMessage('verify-room')
handleVerifyRoom(client: Socket, { roomId }: IRoomRequest): void {
client.join(client.id); //해당 사용자한테만 전송될 수 있게
if (!this.userList[roomId]) {
this.server.to(client.id).emit('is-verify', true);
return;
}
if (Object.keys(this.userList[roomId]['list']).length < this.userList[roomId]['maxHead']) {
this.server.to(client.id).emit('is-verify', true);
return;
}

this.server.to(client.id).emit('is-verify', false);
}

afterInit(server: Server) {
Expand Down

0 comments on commit 7abd682

Please sign in to comment.