more stable connexion
Some checks failed
Linux arm64 / Build (push) Failing after 5m1s

This commit is contained in:
2025-01-26 14:54:46 +01:00
parent e51cc23459
commit 47dc35ebfa
7 changed files with 64 additions and 12 deletions

View File

@@ -12,30 +12,68 @@ public class ConnexionStatusView extends BaseView {
private Client client;
private Server server;
public ConnexionStatusView(StateMachine stateMachine, String address, short port) throws UnknownHostException, IOException {
this(stateMachine, null, new Client(address, port));
private String displayText = "Connecting ...";
public ConnexionStatusView(StateMachine stateMachine, String address, short port)
throws UnknownHostException, IOException {
super(stateMachine);
Thread t = new Thread(() -> {
try {
this.client = new Client(address, port);
bindListeners();
} catch (IOException e) {
e.printStackTrace();
onDisconnect();
}
});
t.start();
}
public ConnexionStatusView(StateMachine stateMachine, short port) throws UnknownHostException, IOException {
this(stateMachine, new Server(port), new Client("localhost", port));
super(stateMachine);
Thread t = new Thread(() -> {
try {
this.server = new Server(port);
this.client = new Client("localhost", port);
bindListeners();
} catch (IOException e) {
e.printStackTrace();
onDisconnect();
}
});
t.start();
}
private void bindListeners() {
this.client.onConnect.connect(this::onConnect);
this.client.onClosed.connect(this::onLeave);
this.client.onDisconnect.connect(this::onDisconnect);
}
private ConnexionStatusView(StateMachine stateMachine, Server server, Client client) {
super(stateMachine);
this.client = client;
this.client.onConnect.connect(this::onConnect);
this.client.onClosed.connect(this::onLeave);
this.server = server;
}
public void onConnect() {
// System.out.println("Connecté");
this.stateMachine.pushState(new MultiPlayerView(stateMachine, client));
}
public void onDisconnect() {
if (client != null) {
String reason = client.getDisconnectReason();
if (reason == null)
displayText = "Le serveur a fermé la connexion !";
else
displayText = "Vous avez été déconnecté ! Raison : " + client.getDisconnectReason();
} else {
displayText = "La connexion a échoué !";
}
}
public void onLeave() {
// System.out.println("Quitté !");
this.client.onDisconnect.clear();
this.client = null;
// on passe le menu de la connexion
this.closeMenu();
@@ -43,7 +81,14 @@ public class ConnexionStatusView extends BaseView {
@Override
public void render() {
ImGui.text("Connecting ...");
ImGui.text(displayText);
renderReturnButton();
}
@Override
public void closeMenu() {
super.closeMenu();
cleanResources();
}
@Override