implement ServerListener
This commit is contained in:
@@ -1,18 +1,38 @@
|
|||||||
package com.university.chat.Client;
|
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 {
|
public class ServerListener implements Runnable {
|
||||||
// TODO: store the ObjectInputStream from the user socket
|
|
||||||
// (this should be the same input stream the
|
private ObjectInputStream in;
|
||||||
// chatClient created when connecting)
|
|
||||||
|
public ServerListener(ObjectInputStream in) {
|
||||||
|
this.in = in;
|
||||||
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void run() {
|
public void run() {
|
||||||
try {
|
try {
|
||||||
// TODO: In an infinite loop read objects from the server
|
while (true) {
|
||||||
// - if it's a ChatMessage -> print "<sender>: <content>"
|
Object object = in.readObject();
|
||||||
// - if it's a FileMessage -> print that a file was received
|
|
||||||
// (filename + sender), it's already
|
if (object instanceof ChatMessage msg) {
|
||||||
// saved to disk by the server.
|
System.out.println(msg.getSender() + ": " + msg.getContent());
|
||||||
|
}
|
||||||
|
|
||||||
|
if (object instanceof FileMessage fileMsg) {
|
||||||
|
System.out.println(
|
||||||
|
"File received: " +
|
||||||
|
fileMsg.getFilename() +
|
||||||
|
" from " +
|
||||||
|
fileMsg.getSender()
|
||||||
|
);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
} catch (Exception e) {
|
} catch (Exception e) {
|
||||||
System.out.println("Disconnected from server");
|
System.out.println("Disconnected from server");
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user