It works !

This commit is contained in:
Clément
2025-03-12 14:26:13 +01:00
parent 3eec95e420
commit 2be11ec4a8
5 changed files with 64 additions and 60 deletions

View File

@@ -4,7 +4,7 @@ import java.net.InetSocketAddress;
public class ChatAppClient {
public static void main(String[] args) {
ClientConsole console = new ClientConsole(new InetSocketAddress("192.168.163.131", 6665));
ClientConsole console = new ClientConsole(new InetSocketAddress("localhost", 6665));
try {
console.joinThread();
} catch (InterruptedException e) {

View File

@@ -1,8 +1,15 @@
package client;
import javafx.application.Platform;
import javafx.fxml.FXML;
import javafx.scene.control.Alert;
import javafx.scene.layout.Border;
import javafx.scene.layout.BorderPane;
import javafx.stage.Stage;
import network.protocol.packets.ServerResponsePacket;
import java.net.InetSocketAddress;
import java.net.SocketException;
import java.time.Instant;
import java.util.List;
@@ -10,30 +17,44 @@ public class ClientGuiController implements ClientListener {
private Client client;
@FXML
private BorderPane vueContainer;
public void setClient(Client client) {
this.client = client;
}
@FXML
public void initialize() {
}
@FXML
public void initialize() throws SocketException {
client = new Client(new InetSocketAddress("localhost", 6665), this, UsernameSingleton.getInstance().getUsername());
Platform.runLater(() -> {
Stage stage = (Stage) vueContainer.getScene().getWindow();
stage.setOnCloseRequest(event -> {
client.close();
});
});
}
@Override
public void handleDisconnect() {
System.out.println("Disconnected");
}
@Override
public void handleConnexionError() {
System.out.println("Connection error");
System.out.println(UsernameSingleton.getInstance().getUsername());
}
@Override
public void handleConnect() {
Platform.runLater(() -> {
Alert alert = new Alert(Alert.AlertType.INFORMATION);
alert.setTitle("Connection");
alert.setHeaderText("Connection to the server successful");
alert.showAndWait();
});
}
@Override
public void handleChatMessage(Instant time, String chatter, String content) {

View File

@@ -15,71 +15,33 @@ import java.util.List;
import static utilities.FxUtilities.centerStage;
public class ClientLoading implements ClientListener {
public class ClientLoading {
@FXML
private Arc spinnerArc;
private Client client;
public void initialize() throws IOException, InterruptedException {
// Création de l'animation de rotation
RotateTransition rotate = new RotateTransition(Duration.seconds(1), spinnerArc);
rotate.setByAngle(360);
rotate.setCycleCount(RotateTransition.INDEFINITE);
rotate.play();
}
public void setClient(Client client) {
this.client = client;
// new Thread(() -> {
// try {
// Thread.sleep(2000);
// login();
// } catch (IOException | InterruptedException e) {
// e.printStackTrace();
// }
// });
}
public void login() throws IOException {
var loader = new FXMLLoader(getClass().getResource("/client/clientVue.fxml"));
loader.load();
client.changeCallback(loader.getController());
((ClientGuiController) loader.getController()).setClient(client);
Stage stage = (Stage) spinnerArc.getScene().getWindow();
stage.setScene(new Scene(loader.getRoot(), 800, 600));
centerStage(stage);
stage.getScene().getStylesheets().add(getClass().getResource("clientStyle.css").toExternalForm());
}
@Override
public void handleDisconnect() {
System.out.println("Disconnected");
}
@Override
public void handleConnexionError() {
System.out.println("Connection error");
}
@Override
public void handleConnect() {
try {
login();
} catch (IOException e) {
e.printStackTrace();
}
}
@Override
public void handleChatMessage(Instant time, String chatter, String content) {
}
@Override
public void handleRoomList(List<String> roomNames) {
}
@Override
public void handleServerResponse(ServerResponsePacket.Response response) {
}
@Override
public void handleActualRoom(String roomName) {
}
}

View File

@@ -5,9 +5,7 @@ import javafx.fxml.FXMLLoader;
import javafx.scene.Scene;
import javafx.scene.control.TextField;
import javafx.stage.Stage;
import client.ClientGui;
import java.io.IOException;
import java.net.InetSocketAddress;
import static utilities.FxUtilities.centerStage;
@@ -21,10 +19,9 @@ public class ClientLogin {
if(username.isEmpty()) {
return;
}
UsernameSingleton.getInstance().setUsername(username);
var loader = new FXMLLoader(getClass().getResource("/client/clientLoading.fxml"));
Client client = new Client(new InetSocketAddress("localhost", 6665), loader.getController(), username);
loader.load();
((ClientLoading) loader.getController()).setClient(client);
Stage stage = (Stage) usernameField.getScene().getWindow();
stage.setScene(new Scene(loader.getRoot(), 800, 600));
centerStage(stage);

View File

@@ -0,0 +1,24 @@
package client;
public class UsernameSingleton {
private static UsernameSingleton instance;
private String username;
private UsernameSingleton() {
}
public static UsernameSingleton getInstance() {
if (instance == null) {
instance = new UsernameSingleton();
}
return instance;
}
public String getUsername() {
return username;
}
public void setUsername(String username) {
this.username = username;
}
}