0
good night.
I need your help in something kind of simple, I’m doing a chat via RMI, the implementation of the program is already working less as I want, but I’m breaking my head in a simple print. I need to arrange the exit so it stays the following way:
Chat: type a message
so-and-so says: typed message
My program is a thread to read messages from the server, and it is precisely these messages that I am unable to organize.
currently the messages are coming out as follows:
Chat: Enter a message
Chat: so-and-so says: message typed
I mean, I’d like to follow the sequence
Message
Meshing
I know it’s kind of dumb that doubt, but I’m really not getting to do this kkkk follows my code:
import java.rmi.Naming;
import java.rmi.RemoteException;
import java.util.Scanner;
public class ChatCliente{
private static Scanner scanner;
public static void main(String[] args) {
try {
ChatIF chat = (ChatIF) Naming.lookup("rmi://localhost:1098/Chat"); //Buscando a Interface no Servidor
String nome;
String msg = "";
scanner = new Scanner(System.in);
int cont = chat.getmensagens().size();
//Recebendo o nome do Usuario
System.out.println("---------------------------------------------");
System.out.println(" SEJA BEM VINDO AO CHAT ");
System.out.println("---------------------------------------------");
System.out.print("[Chat] Por Favor, Digite seu nome: ");
nome = scanner.nextLine();
//Thread responsavel por ler as mensagens do servidor
Thread thread = new Thread(new Runnable() {
int cont = chat.getmensagens().size();
@Override
public void run() {
try {
while(true){
if (chat.getmensagens().size() > cont){
System.out.println(chat.getmensagens().get(chat.getmensagens().size()-1));
cont++;
}
}
}catch (RemoteException e) {
e.printStackTrace();
}
}
});
System.out.println("---------------------------------------------");
System.out.println("Ola, " + nome + "! Voce agora esta conectado!");
System.out.println("---------------------------------------------");
thread.start();
//Envia as mensagens para o servidor
while(true){
System.out.print("[Chat]"+": ");
msg = scanner.nextLine();
chat.setMensagem(nome+" diz"+": "+msg);
}
} catch (Exception e) {
System.out.println("Ocorreu um erro: " + e);
}
}
}