update clientSession class

This commit is contained in:
2026-06-14 10:04:49 +03:30
parent e0de21553d
commit 6e92929f8f
@@ -84,13 +84,24 @@ public class ClientSession implements Runnable {
private void handleChatMessage(ChatMessage msg) throws IOException { private void handleChatMessage(ChatMessage msg) throws IOException {
switch (msg.getType()) { switch (msg.getType()) {
case PUBLIC_MESSAGE -> { case PUBLIC_MESSAGE -> {
// TODO: Broadcast this message to every connected client.
for (ClientSession session: userManager.getAllSessions()) {
session.sendMessage(msg);
}
} }
case PRIVATE_MESSAGE -> { case PRIVATE_MESSAGE -> {
// TODO: Forward this message to the receiver user.
ClientSession receiver = userManager.getUser(msg.getSender());
if (receiver != null) {
receiver.sendMessage(msg);
}
else {
sendMessage(new ChatMessage(MessageType.PUBLIC_MESSAGE, "Server", username, "User '" + msg.getReceiver() + "' is not online."));
}
} }
case USER_LIST -> { case USER_LIST -> {
// TODO: Reply to the requester with the list of online users.
sendMessage(new ChatMessage(MessageType.USER_LIST, "Server", username, userManager.listUsers()));
} }
} }
} }
@@ -103,6 +114,15 @@ public class ClientSession implements Runnable {
Files.write(sentPath, fileMsg.getData()); Files.write(sentPath, fileMsg.getData());
Files.write(recvPath, fileMsg.getData()); Files.write(recvPath, fileMsg.getData());
// TODO: Forward the received file-message to the destination user. ClientSession receiver = userManager.getUser(fileMsg.getReceiver());
if (receiver != null) {
receiver.sendMessage(fileMsg);
} else {
sendMessage(new ChatMessage(
MessageType.PUBLIC_MESSAGE,
"Server", username,
"User '" + fileMsg.getReceiver() + "' is not online. File saved on server."
));
}
} }
} }