Skip to content

Commit

Permalink
feat: game logo and net implementing
Browse files Browse the repository at this point in the history
  • Loading branch information
LapisBerry committed May 21, 2024
1 parent fbc8bc7 commit 6ada5fe
Show file tree
Hide file tree
Showing 5 changed files with 62 additions and 2 deletions.
3 changes: 3 additions & 0 deletions src/main/java/com/lapisberry/Main.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import com.lapisberry.gui.scenes.JoinScene;
import javafx.application.Application;
import javafx.scene.image.Image;
import javafx.stage.Stage;

public class Main extends Application {
Expand All @@ -14,6 +15,8 @@ public static void main(String[] args) {
@Override
public void start(Stage stage) {
primaryStage = stage;
primaryStage.setTitle("MaKillMai");
primaryStage.getIcons().add(new Image(ClassLoader.getSystemResource("images/makillmai-icon.png").toString()));
primaryStage.setScene(new JoinScene());

primaryStage.show();
Expand Down
24 changes: 24 additions & 0 deletions src/main/java/com/lapisberry/net/Client.java
Original file line number Diff line number Diff line change
Expand Up @@ -27,5 +27,29 @@ public Client(final String host) {
// Methods
@Override
public void run() {
new Thread(this::startListeningServerPacket, "Listening server packet thread").start();
}

private void startListeningServerPacket() {
while (!socket.isClosed()) {
try {
Object packet = inputStream.readObject();
System.out.println("Packet received from server: " + packet);
} catch (IOException e) {
System.out.println("Server disconnected.");
break;
} catch (ClassNotFoundException e) {
System.out.println("Packet from server cannot be read.");
}
}
}

private void sendPacketToServer(Object packet) {
try {
outputStream.writeObject(packet);
outputStream.flush();
} catch (IOException e) {
System.out.println("Packet cannot be sent to server.");
}
}
}
33 changes: 31 additions & 2 deletions src/main/java/com/lapisberry/net/ClientHandler.java
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,8 @@

public class ClientHandler implements Runnable {
// Fields
private Server server;
private Socket socket;
private final Server server;
private final Socket socket;
private ObjectOutputStream outputStream;
private ObjectInputStream inputStream;

Expand All @@ -29,5 +29,34 @@ public ClientHandler(Server server, Socket socket) {
// Methods
@Override
public void run() {
new Thread(this::startListeningClientPacket, "Listening client packet thread").start();
}

private void startListeningClientPacket() {
while (!socket.isClosed()) {
try {
Object packet = inputStream.readObject();
server.processPacketFromClient(this, packet);
} catch (IOException e) {
System.out.println("Client disconnected: " + socket.getInetAddress().getHostAddress());
break;
} catch (ClassNotFoundException e) {
System.out.println("Packet from client cannot be read.");
}
}
}

private void sendPacketToClient(Object packet) {
try {
outputStream.writeObject(packet);
outputStream.flush();
} catch (IOException e) {
System.out.println("Packet cannot be sent to client.");
}
}

// Getters Setters
public Socket getSocket() {
return socket;
}
}
4 changes: 4 additions & 0 deletions src/main/java/com/lapisberry/net/Server.java
Original file line number Diff line number Diff line change
Expand Up @@ -43,4 +43,8 @@ private void startAcceptingClients() {
}
}
}

public void processPacketFromClient(ClientHandler sender, Object packet) {
System.out.println("Processing packet from " + sender.getSocket().getInetAddress().getHostAddress() + ": " + packet);
}
}
Binary file added src/main/resources/images/makillmai-icon.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.

0 comments on commit 6ada5fe

Please sign in to comment.