Publish disconnected user info on unsubscribe

This commit is contained in:
Arnaud Vergnet 2021-01-25 15:02:00 +01:00
parent 33f355b90b
commit ebd292e6eb

View file

@ -3,6 +3,7 @@ package fr.insa.clavardator.server;
import fr.insa.clavardator.lib.network.TcpConnection;
import fr.insa.clavardator.lib.network.TcpListener;
import fr.insa.clavardator.lib.users.UserInformation;
import fr.insa.clavardator.lib.users.UserState;
import fr.insa.clavardator.lib.util.Log;
import java.io.EOFException;
@ -58,7 +59,7 @@ public class Presence {
user.receive(msg -> Log.w(getClass().getSimpleName(), "Received an unexpected message " + msg),
e -> {
if (e instanceof EOFException) {
unsubscribe(userInformation.id);
unsubscribe(userInformation);
}
});
} else {
@ -71,11 +72,12 @@ public class Presence {
});
}
public void unsubscribe(String id) {
Log.v(getClass().getSimpleName(), "Unsubscribing user " + id);
subscribers.get(id).close();
subscribers.remove(id);
connectedUsers.removeIf(userInformation -> userInformation.id.equals(id));
public void unsubscribe(UserInformation userInformation) {
Log.v(getClass().getSimpleName(), "Unsubscribing user " + userInformation.id + "(" + userInformation.getUsername() + ")");
subscribers.get(userInformation.id).close();
subscribers.remove(userInformation.id);
publish(new UserInformation(userInformation.id, userInformation.getUsername(), UserState.DISCONNECTED));
connectedUsers.removeIf(ui -> userInformation.id.equals(ui.id));
}
public void stop() {