Tenho algumas dificuldades em compreender este pedaço de código que dei numa aula.
Gostava de ver várias interpretações acerca do código!
public void run() {
try {
BufferedReader in = new BufferedReader(new InputStreamReader(conect.getInputStream()));
PrintStream out = new PrintStream(ligacao.getOutputStream());
ct = in.readLine();
if (ct == null) {return;}
clientes.add(out);
String escrever = in.readLine();
if (escrever.equals("sair")){
ligacao.close();
sendToAll(out, " saiu ", "do chat!");
clientes.remove(out);
ligacao.close();
}
while (escrever != null && !(escrever.trim().equals("")) && !(escrever.equals("sair"))) {
sendToAll(out, " ("+clientes.size()+ " online): ", msg);
System.out.println(escrever);
escrever = in.readLine();
}
sendToAll(out, " saiu ", "do chat!");
clientes.remove(out);
ligacao.close();
} catch (IOException e) {
System.out.println("IOException: " + e);
}
}
// enviar uma mensagem para todos, menos para o próprio
public void sendToAll(PrintStream out, String acontecimento,
String escrever) throws IOException {
Enumeration e = clientes.elements();
while (e.hasMoreElements()) {
PrintStream chat = (PrintStream) e.nextElement();
if (chat != out) {
chat.println(ct + acontecimento + escrever);
}
}
}
}
[color="red"]Editado por Felipe: use bbcode para facilitar a leitura do codigo[/color]