Develop #1
@@ -1,18 +1,34 @@
|
||||
package com.university.chat.Client;
|
||||
|
||||
import com.university.chat.Common.ChatMessage;
|
||||
import com.university.chat.Common.FileMessage;
|
||||
|
||||
import java.io.ObjectInputStream;
|
||||
import java.net.Socket;
|
||||
|
||||
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 obj = in.readObject();
|
||||
if (obj instanceof ChatMessage msg) {
|
||||
System.out.println(msg.getSender() + ": " + msg.getContent());
|
||||
}
|
||||
else if (obj instanceof FileMessage file) {
|
||||
System.out.println("[File received] '" + file.getFilename()
|
||||
+ "' from " + file.getSender());
|
||||
}
|
||||
}
|
||||
|
||||
} catch (Exception e){
|
||||
System.out.println("Disconnected from server");
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user