package com.rila.chat.client;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.net.Socket;
import java.util.LinkedList;
import java.util.List;
import fr.cesi.chat.domain.Message;
/**
* Permet de recevoir les messages du serveur
*
* @author Florian
*
*/
public class ClientThreadServerResponseMessage implements Runnable {
public static final int port = 6667;
private Socket socket;
private ObjectInputStream in = null;
private String ipServeur;
private List<Message> messages = new LinkedList<Message>();
public ClientThreadServerResponseMessage(String ipServeur) {
this.ipServeur = ipServeur;
}
@Override
public void run() {
try {
socket = new Socket(ipServeur, port);
in = new ObjectInputStream(socket.getInputStream());
// Affiche les messages reçus
while (true) {
Message message = (Message) in.readObject();
// System.out.println(" - " + message.getMessage());
messages.add(message);
Thread.sleep(1000); // 1sec
}
} catch (IOException e) {
e.printStackTrace();
} catch (InterruptedException e) {
e.printStackTrace();
} catch (ClassNotFoundException e) {
e.printStackTrace();
}
}
public List<Message> getMessages() {
return messages;
}
public Socket getSocket() {
return socket;
}
}