From 0e17f669e1c2d4a6ea01d85a1440d23786ede70a Mon Sep 17 00:00:00 2001 From: LapisBerry Date: Tue, 21 May 2024 11:49:44 +0700 Subject: [PATCH] feat: Server sendPacketToAllClients() --- src/main/java/com/lapisberry/net/ClientHandler.java | 3 ++- src/main/java/com/lapisberry/net/Server.java | 6 ++++++ 2 files changed, 8 insertions(+), 1 deletion(-) diff --git a/src/main/java/com/lapisberry/net/ClientHandler.java b/src/main/java/com/lapisberry/net/ClientHandler.java index 1a3df4f..5046b39 100644 --- a/src/main/java/com/lapisberry/net/ClientHandler.java +++ b/src/main/java/com/lapisberry/net/ClientHandler.java @@ -1,5 +1,6 @@ package com.lapisberry.net; +import java.io.EOFException; import java.io.IOException; import java.io.ObjectInputStream; import java.io.ObjectOutputStream; @@ -46,7 +47,7 @@ private void startListeningClientPacket() { } } - private void sendPacketToClient(Object packet) { + public void sendPacketToClient(Object packet) { try { outputStream.writeObject(packet); outputStream.flush(); diff --git a/src/main/java/com/lapisberry/net/Server.java b/src/main/java/com/lapisberry/net/Server.java index 20cdbc2..4a35df5 100644 --- a/src/main/java/com/lapisberry/net/Server.java +++ b/src/main/java/com/lapisberry/net/Server.java @@ -47,4 +47,10 @@ private void startAcceptingClients() { public void processPacketFromClient(ClientHandler sender, Object packet) { System.out.println("Processing packet from " + sender.getSocket().getInetAddress().getHostAddress() + ": " + packet); } + + private void sendPacketToAllClients(Object packet) { + for (ClientHandler clientHandler : clientHandlers) { + clientHandler.sendPacketToClient(packet); + } + } } \ No newline at end of file