feat(Event): 2.6.6: Gestion des déconnexions

This commit is contained in:
Emi Boucly 2025-01-07 21:15:38 +01:00
parent 1732a63a4a
commit 0b513c19bc
3 changed files with 14 additions and 5 deletions

View file

@ -99,6 +99,7 @@ public class ChatController implements Initializable {
avatarMenuItem.setOnAction(this::handleAvatarChange); avatarMenuItem.setOnAction(this::handleAvatarChange);
avatarImageView.setOnMouseClicked(this::handleAvatarChange); avatarImageView.setOnMouseClicked(this::handleAvatarChange);
sendButton.setOnAction(this::onActionSend); sendButton.setOnAction(this::onActionSend);
messageTextField.setOnAction(this::onActionSend);
initContactListView(); initContactListView();
initPostListView(); initPostListView();
@ -157,7 +158,7 @@ public class ChatController implements Initializable {
contactMap.put(this.contact.getLogin(), this.contact); contactMap.put(this.contact.getLogin(), this.contact);
LOGGER.info("Nouveau contact : " + contact); LOGGER.info("Nouveau contact : " + contact);
LOGGER.info(contactMap.toString()); LOGGER.info(contactMap.toString());
Matcher matcher = hostPortPattern.matcher("localhost:2024"); Matcher matcher = hostPortPattern.matcher(hostComboBox.getValue());
matcher.matches(); matcher.matches();
String host = matcher.group(1); String host = matcher.group(1);
int port = (matcher.group(2) != null) ? Integer.parseInt(matcher.group(2)) : 2024; int port = (matcher.group(2) != null) ? Integer.parseInt(matcher.group(2)) : 2024;
@ -181,9 +182,9 @@ public class ChatController implements Initializable {
connectionButton.setSelected(false); connectionButton.setSelected(false);
} }
} else if (!connectionButton.isSelected()) { } else if (!connectionButton.isSelected()) {
this.client.sendQuitEvent();
clearLists(); clearLists();
if (this.client.isConnected()) { if (this.client.isConnected()) {
this.client.close();
this.contact.setConnected(false); this.contact.setConnected(false);
} }
statusLabel.setText("not connected to " + hostComboBox.getValue()); statusLabel.setText("not connected to " + hostComboBox.getValue());
@ -300,11 +301,8 @@ public class ChatController implements Initializable {
if (content.getString("to").equals(((Contact) contactsListView.getSelectionModel().getSelectedItem()).getLogin()) || if (content.getString("to").equals(((Contact) contactsListView.getSelectionModel().getSelectedItem()).getLogin()) ||
content.getString("from").equals(loginTextField.getText())) { content.getString("from").equals(loginTextField.getText())) {
postVector.add(Post.fromJson(content)); postVector.add(Post.fromJson(content));
System.out.println(postVector);
postsObservableList.add(Post.fromJson(content)); postsObservableList.add(Post.fromJson(content));
postListView.refresh(); postListView.refresh();
System.out.println(postsObservableList);
System.out.println(postListView);
} }
} }
@ -322,6 +320,7 @@ public class ChatController implements Initializable {
content, content,
new File("src/main/resources/rtgre/chat/avatars.png") new File("src/main/resources/rtgre/chat/avatars.png")
); );
System.out.println(user.getAvatar());
contactMap.add(user); contactMap.add(user);
contactObservableList.add(user); contactObservableList.add(user);
LOGGER.info(contactMap.toString()); LOGGER.info(contactMap.toString());

View file

@ -68,6 +68,11 @@ public class ChatClient extends ClientTCP {
sendEvent(listPostEvent); sendEvent(listPostEvent);
} }
public void sendQuitEvent() {
Event quitEvent = new Event(Event.QUIT, new JSONObject());
sendEvent(quitEvent);
}
@Override @Override
public void receiveLoop() { public void receiveLoop() {

View file

@ -254,6 +254,9 @@ public class ChatServer {
doListPost(event.getContent()); doListPost(event.getContent());
LOGGER.info("Sending Posts"); LOGGER.info("Sending Posts");
return true; return true;
} else if (event.getType().equals(Event.QUIT)) {
LOGGER.info("Déconnexion");
return false;
} else { } else {
LOGGER.warning("Unhandled event type: " + event.getType()); LOGGER.warning("Unhandled event type: " + event.getType());
return false; return false;
@ -367,6 +370,8 @@ public class ChatServer {
try { try {
sock.close(); sock.close();
removeClient(this); removeClient(this);
user.setConnected(false);
sendEventToAllContacts(new Event(Event.CONT, user.toJsonObject()));
} catch (IOException e) { } catch (IOException e) {
throw new RuntimeException(e); throw new RuntimeException(e);
} }