forked from AdvancedProgramming1404/HW-10-Socket-Programming
ServerListener completed.
This commit is contained in:
@@ -1,24 +1,38 @@
|
||||
package com.university.chat.Client;
|
||||
|
||||
import com.university.chat.Common.ChatMessage;
|
||||
import com.university.chat.Common.FileMessage;
|
||||
import com.university.chat.Server.ClientSession;
|
||||
|
||||
import java.io.ObjectInputStream;
|
||||
|
||||
public class ServerListener implements Runnable{
|
||||
|
||||
private final ObjectInputStream in;
|
||||
|
||||
public ServerListener(ObjectInputStream in) {
|
||||
this.in = in;
|
||||
}
|
||||
// TODO: store the ObjectInputStream from the user socket
|
||||
// (this should be the same input stream the
|
||||
// chatClient created when connecting)
|
||||
|
||||
@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 incomingObject = in.readObject();
|
||||
|
||||
if (incomingObject instanceof ChatMessage) {
|
||||
ChatMessage message = (ChatMessage) incomingObject;
|
||||
System.out.println(message.getSender() + ": " + message.getContent());
|
||||
|
||||
} else if (incomingObject instanceof FileMessage) {
|
||||
FileMessage fileMessage = (FileMessage) incomingObject;
|
||||
System.out.println("[FILE RECEIVE] You received a file: '"
|
||||
+ fileMessage.getFilename() + "' from " + fileMessage.getSender()
|
||||
+ " (Saved on server disk).");
|
||||
}
|
||||
}
|
||||
} catch (Exception e){
|
||||
System.out.println("Disconnected from server");
|
||||
System.out.println("Disconnected from server.");
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -15,7 +15,7 @@ import java.util.Scanner;
|
||||
|
||||
public class chatClient {
|
||||
public static void main() throws IOException {
|
||||
|
||||
|
||||
String host = "localhost";
|
||||
int port = 8080;
|
||||
Scanner scanner = new Scanner(System.in);
|
||||
|
||||
Reference in New Issue
Block a user