Skip to content

Commit

Permalink
feat/#110 :: 빈 방 정리 기능 구현
Browse files Browse the repository at this point in the history
 Co-Authored-By: Kiyoung <[email protected]>
  • Loading branch information
youn9k committed Nov 26, 2024
1 parent 8e23c4d commit 2f25021
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 22 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,12 @@ final class RoomManager {
.failure(RoomError.joinFailed)
}

func cleanRoom() -> Int {
let emptyRoomCount = rooms.filter { $0.userList.isEmpty }.count
rooms.removeAll { $0.userList.isEmpty }
return emptyRoomCount
}

private func randomRoomID() -> String {
return "room-\(UUID().uuidString)"
}
Expand Down
43 changes: 21 additions & 22 deletions PhotoGetherServer/PhotoGetherServer/Sources/App/routes.swift
Original file line number Diff line number Diff line change
Expand Up @@ -13,36 +13,32 @@ func routes(_ app: Application) throws {
request,
client in
connectedClients.append(client)

// 클라이언트가 연결될 때 호출
print("Client connected. Total connected clients: \(connectedClients.count)")

// 클라이언트로부터 데이터를 수신할 때 호출
client.onBinary {
client,
data in
print("Received binary data of size: \(data.readableBytes)")
// TODO: 1. data -> JSON으로 디코딩
guard let requestType = data.toDTO(
type: WebSocketRequestType.self,
decoder: decoder
) else {
print("Decode Failed to WebSocketRequestType: \(data)")
print("[DEBUG] :: Decode Failed to WebSocketRequestType: \(data)")
return
}

switch requestType.messageType {
case "signaling":
printRequestLog(requestType, data: data)
guard let request = data.toDTO(
type: SignalingRequestDTO.self,
decoder: decoder
) else {
print("Decode Failed to SignalingRequestDTO: \(data)")
print("[DEBUG] :: Decode Failed to SignalingRequestDTO: \(data)")
return
}

guard let data = request.message else {
print("Message is Nil")
print("[DEBUG] :: Message is Nil")
return
}

Expand All @@ -51,14 +47,15 @@ func routes(_ app: Application) throws {
.forEach { $0.send(data) }

case "createRoom":
printRequestLog(requestType, data: data)
let ids = roomManager.createRoom(client)
let dto = CreateRoomResponseDTO(
roomID: ids.roomID,
hostID: ids.userID
)

guard let message = dto.toData(encoder) else {
print("Encode Failed to Data: \(dto)")
print("[DEBUG] :: Encode Failed to Data: \(dto)")
return
}

Expand All @@ -68,26 +65,27 @@ func routes(_ app: Application) throws {
)

guard let response = responseDTO.toData(encoder) else {
print("Encode Failed to Data: \(responseDTO)")
print("[DEBUG] :: Encode Failed to Data: \(responseDTO)")
return
}

client.send(response)

case "joinRoom":
printRequestLog(requestType, data: data)
guard let request = data.toDTO(
type: RoomRequestDTO.self,
decoder: decoder
) else {
print("Decode Failed to RoomRequestDTO: \(data)")
print("[DEBUG] :: Decode Failed to RoomRequestDTO: \(data)")
return
}

guard let message = request.message?.toDTO(
type: JoinRoomRequestMessage.self,
decoder: decoder
) else {
print("Decode Failed to DTO: \(request.message)")
print("[DEBUG] :: Decode Failed to DTO: \(request.message)")
return
}

Expand All @@ -96,7 +94,7 @@ func routes(_ app: Application) throws {
switch joinResult {
case .success(let responseDTO):
guard let message = responseDTO.toData(encoder) else {
print("Encode Failed to Data: \(responseDTO)")
print("[DEBUG] :: Encode Failed to Data: \(responseDTO)")
return
}

Expand All @@ -106,7 +104,7 @@ func routes(_ app: Application) throws {
)

guard let response = responseDTO.toData(encoder) else {
print("Encoder Failed to Data: \(responseDTO)")
print("[DEBUG] :: Encoder Failed to Data: \(responseDTO)")
return
}

Expand All @@ -117,26 +115,27 @@ func routes(_ app: Application) throws {
let failureResponseDTO = RoomResponseDTO(messageType: .joinRoom)

guard let response = failureResponseDTO.toData(encoder) else {
print("Encoder Failed to Data: \(failureResponseDTO)")
print("[DEBUG] :: Encoder Failed to Data: \(failureResponseDTO)")
return
}

client.send(response)
}
default:
print("Unknown request message type: \(requestType.messageType)")
print("[SYSTEM] :: Unknown request message type: \(requestType.messageType)")
}
// TODO: 2. type을 보고 수행할 명령을 선택

// connectedClients
// .filter { $0 !== client }
// .forEach { $0.send(data) }
}

// 클라이언트가 연결을 종료할 때 호출
client.onClose.whenComplete { _ in
connectedClients.removeAll { $0 === client }
print("Client disconnected. Total connected clients: \(connectedClients.count)")
let cleanedCount = roomManager.cleanRoom()
print("[SYSTEM] :: Room Cleaned: \(cleanedCount)")
print("[SYSTEM] :: Client disconnected. Total connected clients: \(connectedClients.count)")
}
}

func printRequestLog(_ requestType: WebSocketRequestType, data: ByteBuffer) {
print("[REQUEST] :: \(requestType.messageType) Request Received: \(data.readableBytes)")
}
}

0 comments on commit 2f25021

Please sign in to comment.