implementation

This commit is contained in:
2026-06-07 20:20:49 +04:30
parent 4a616ef007
commit 53e40ca2f5
2 changed files with 11 additions and 10 deletions
+10 -9
View File
@@ -42,11 +42,11 @@ public class ChatServer {
// step 1: iterate through connectedClients safely // step 1: iterate through connectedClients safely
// step 2: skip the sender client // step 2: skip the sender client
// step 3: send message to remaining clients using sendMessage // step 3: send message to remaining clients using sendMessage
for(ClientHandler client: connectedClients) synchronized (connectedClients) {
{ for (ClientHandler client : connectedClients) {
if(!client.getUsername().equals(sender.getUsername())) if (client != sender) {
{ client.sendMessage(message);
client.sendMessage(message); }
} }
} }
} }
@@ -54,9 +54,10 @@ public class ChatServer {
public void broadcastSystemMessage(String message) { public void broadcastSystemMessage(String message) {
// step 1: iterate through all connected clients // step 1: iterate through all connected clients
// step 2: send system message to each client // step 2: send system message to each client
for(ClientHandler client: connectedClients) synchronized (connectedClients) {
{ for (ClientHandler client : connectedClients) {
client.sendMessage(message); client.sendMessage(message);
}
} }
} }
@@ -65,7 +66,7 @@ public class ChatServer {
// step 2: retrieve username from clientHandler // step 2: retrieve username from clientHandler
// step 3: broadcast system message about user leaving // step 3: broadcast system message about user leaving
connectedClients.remove(clientHandler); connectedClients.remove(clientHandler);
broadcastSystemMessage(clientHandler.getUsername() + " left"); broadcastSystemMessage("[System] " + clientHandler.getUsername() + " left the chat");
} }
public static void main(String[] args) { public static void main(String[] args) {
+1 -1
View File
@@ -23,7 +23,7 @@ public class MessageListener implements Runnable {
} }
} catch (IOException e) { } catch (IOException e) {
throw new RuntimeException(e); System.err.println(e.getMessage());
} }
} }
} }