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

View file

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

View file

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