implement ServerListener.java
This commit is contained in:
@@ -1,9 +1,19 @@
|
|||||||
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
|
// TODO: store the ObjectInputStream from the user socket
|
||||||
// (this should be the same input stream the
|
// (this should be the same input stream the
|
||||||
// chatClient created when connecting)
|
// chatClient created when connecting)
|
||||||
|
private ObjectInputStream inputStream;
|
||||||
|
|
||||||
|
public ServerListener(ObjectInputStream inputStream) {
|
||||||
|
this.inputStream = inputStream;
|
||||||
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void run() {
|
public void run() {
|
||||||
@@ -13,6 +23,16 @@ public class ServerListener implements Runnable{
|
|||||||
// - if it's a FileMessage -> print that a file was received
|
// - if it's a FileMessage -> print that a file was received
|
||||||
// (filename + sender), it's already
|
// (filename + sender), it's already
|
||||||
// saved to disk by the server.
|
// saved to disk by the server.
|
||||||
|
while (true) {
|
||||||
|
Object object = inputStream.readObject();
|
||||||
|
|
||||||
|
if(object instanceof ChatMessage) {
|
||||||
|
System.out.println(((ChatMessage) object).getSender() + ": " + ((ChatMessage) object).getContent());
|
||||||
|
}
|
||||||
|
else if(object instanceof FileMessage) {
|
||||||
|
System.out.println("File received: " + ((FileMessage) object).getFilename() + " from " + ((FileMessage) object).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