forked from AdvancedProgramming1404/HW-10-Socket-Programming
complete client
This commit is contained in:
@@ -2,16 +2,37 @@ package com.university.chat.Server;
|
||||
|
||||
import com.university.chat.Common.ChatMessage;
|
||||
import com.university.chat.Common.FileMessage;
|
||||
import com.university.chat.Common.MessageType;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.io.ObjectInputStream;
|
||||
import java.io.ObjectOutputStream;
|
||||
import java.net.Socket;
|
||||
import java.nio.file.Files;
|
||||
|
||||
public class ClientSession implements Runnable {
|
||||
|
||||
private String username;
|
||||
private ObjectOutputStream out;
|
||||
private ObjectInputStream in;
|
||||
private Socket socket;
|
||||
private UserManager userManage;
|
||||
|
||||
|
||||
|
||||
public ClientSession(Socket socket, UserManager userManager) {
|
||||
this.socket = socket;
|
||||
this.userManage = userManager;
|
||||
|
||||
try {
|
||||
out = new ObjectOutputStream(socket.getOutputStream());
|
||||
out.flush();
|
||||
in = new ObjectInputStream(socket.getInputStream());
|
||||
} catch (IOException e) {
|
||||
throw new RuntimeException(e);
|
||||
}
|
||||
|
||||
|
||||
// TODO : Create an ObjectOutputStream from socket.getOutputStream()
|
||||
// and an ObjectInputStream from socket.getInputStream().
|
||||
}
|
||||
@@ -20,6 +41,24 @@ public class ClientSession implements Runnable {
|
||||
public void run() {
|
||||
try {
|
||||
|
||||
|
||||
ChatMessage loginmsg = (ChatMessage) in.readObject();
|
||||
String username = loginmsg.getSender();
|
||||
|
||||
|
||||
if(loginmsg.getType() != MessageType.LOGIN){
|
||||
out.writeObject(new ChatMessage(MessageType.LOGIN_FAILED , "server"
|
||||
, username,"login failed"));
|
||||
socket.close();
|
||||
return;
|
||||
}
|
||||
|
||||
out.writeObject(new ChatMessage(MessageType.LOGIN_SUCCESS , "server" ,
|
||||
null , "welcome to messanger"));
|
||||
|
||||
userManage.addUser(username , this);
|
||||
|
||||
|
||||
// TODO: Welcome the user (login step)
|
||||
// 1. Read the first object sent by the client.
|
||||
// 2. Check it's a ChatMessage with type LOGIN.
|
||||
|
||||
Reference in New Issue
Block a user