-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathAtendenteCliente.java
More file actions
79 lines (65 loc) · 3.22 KB
/
AtendenteCliente.java
File metadata and controls
79 lines (65 loc) · 3.22 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.io.IOException;
import java.net.Socket;
public class AtendenteCliente implements Runnable {
private Socket socketDoCliente;
private GerenciadorDeClientes gerenciador;
private PrintWriter paraCliente;
private String nomeCliente;
public AtendenteCliente(Socket socketDoCliente, GerenciadorDeClientes gerenciador) {
this.socketDoCliente = socketDoCliente;
this.gerenciador = gerenciador;
}
@Override
public void run() {
try {
this.paraCliente = new PrintWriter(socketDoCliente.getOutputStream(), true);
BufferedReader doCliente = new BufferedReader(new InputStreamReader(socketDoCliente.getInputStream()));
paraCliente.println("Bem-vindo! Use /msg 'Apelido do Usuario' 'Mensagem' para mandar mensagens privadas.");
paraCliente.println("Digite seu apelido para entrar:");
String apelido = doCliente.readLine();
if (apelido == null || apelido.trim().isEmpty()) {
apelido = "Anonimo_" + socketDoCliente.getPort();
}
this.nomeCliente = apelido;
gerenciador.adicionarCliente(this.nomeCliente, this.paraCliente);
String infoOnline = gerenciador.getListaConectados();
paraCliente.println("------------------------------------------");
paraCliente.println(infoOnline);
paraCliente.println("------------------------------------------");
String msgEntrada = "--- " + this.nomeCliente + " entrou no chat ---";
System.out.println(msgEntrada);
gerenciador.enviarBroadcast(msgEntrada, this.nomeCliente);
String linhaRecebida;
while ((linhaRecebida = doCliente.readLine()) != null) {
if ("sair".equalsIgnoreCase(linhaRecebida.trim())) {
break;
}
if (linhaRecebida.startsWith("/msg ")) {
String[] partes = linhaRecebida.split(" ", 3);
if (partes.length == 3) {
String destinatario = partes[1];
String mensagem = partes[2];
gerenciador.enviarMensagemPrivada(destinatario, mensagem, this.nomeCliente);
} else {
paraCliente.println("Erro: Use /msg [Nome] [Mensagem]");
}
}
else {
String msgFormatada = "[" + this.nomeCliente + "]: " + linhaRecebida;
gerenciador.enviarBroadcast(msgFormatada, this.nomeCliente);
}
}
} catch (IOException e) {
System.out.println("Erro na conexão com " + nomeCliente);
} finally {
if (nomeCliente != null) {
gerenciador.removerCliente(nomeCliente);
gerenciador.enviarBroadcast("--- " + nomeCliente + " saiu do chat ---", "Servidor");
}
try { socketDoCliente.close(); } catch (IOException e) {}
}
}
}