From 969d67d210fe88b3635b72965a75eafec14171a9 Mon Sep 17 00:00:00 2001 From: LapisBerry Date: Thu, 23 May 2024 11:05:06 +0700 Subject: [PATCH] feat: ClientHandler automatically remove --- src/main/java/com/lapisberry/net/ClientHandler.java | 12 ++++++++++++ src/main/java/com/lapisberry/net/Server.java | 6 ++++-- 2 files changed, 16 insertions(+), 2 deletions(-) diff --git a/src/main/java/com/lapisberry/net/ClientHandler.java b/src/main/java/com/lapisberry/net/ClientHandler.java index 2e4a2f7..6c23d9d 100644 --- a/src/main/java/com/lapisberry/net/ClientHandler.java +++ b/src/main/java/com/lapisberry/net/ClientHandler.java @@ -50,6 +50,7 @@ private void startListeningClientPacket() { System.out.println("Packet from client cannot be read."); } } + close(); } private void sendPacketToClient(ServerPacket packet) { @@ -61,6 +62,17 @@ private void sendPacketToClient(ServerPacket packet) { } } + public void close() { + try { + socket.close(); + outputStream.close(); + inputStream.close(); + } catch (IOException e) { + System.out.println("ClientHandler cannot be closed."); + } + server.removeClientHandler(this); + } + // Getters Setters public Socket getSocket() { return socket; diff --git a/src/main/java/com/lapisberry/net/Server.java b/src/main/java/com/lapisberry/net/Server.java index 1439cb4..66a7b70 100644 --- a/src/main/java/com/lapisberry/net/Server.java +++ b/src/main/java/com/lapisberry/net/Server.java @@ -3,8 +3,6 @@ import com.lapisberry.game.controllers.GameController; import com.lapisberry.game.controllers.LobbyController; import com.lapisberry.net.packets.ClientPacket; -import com.lapisberry.net.packets.JoinResponsePacket; -import com.lapisberry.net.packets.ServerPacket; import com.lapisberry.utils.Config; import java.io.IOException; @@ -65,4 +63,8 @@ public void close() { System.out.println("Server cannot be closed."); } } + + public void removeClientHandler(ClientHandler clientHandler) { + clientHandlers.remove(clientHandler); + } } \ No newline at end of file