implementing ServerListner.java

This commit is contained in:
2026-07-17 18:28:22 +03:30
parent 87a5ca499d
commit 74f963ff8e
@@ -1,18 +1,27 @@
package com.university.chat.Client;
import com.university.chat.Common.ChatMessage;
import com.university.chat.Common.FileMessage;
import java.io.ObjectInputStream;
public class ServerListener implements Runnable{
// TODO: store the ObjectInputStream from the user socket
// (this should be the same input stream the
// chatClient created when connecting)
private final ObjectInputStream in;
public ServerListener(ObjectInputStream in) {
this.in = in;
}
@Override
public void run() {
try {
// TODO: In an infinite loop read objects from the server
// - if it's a ChatMessage -> print "<sender>: <content>"
// - if it's a FileMessage -> print that a file was received
// (filename + sender), it's already
// saved to disk by the server.
while(true){
Object recived = in.readObject();
if(recived instanceof ChatMessage msg)
System.out.println(msg.getSender() + "==> " + msg.getContent());
else if(recived instanceof FileMessage fileMsg)
System.out.println(fileMsg.getSender() + "sent ==> " +fileMsg.getFilename());
}
} catch (Exception e){
System.out.println("Disconnected from server");
}