From b0f71c097631f6f9d595865c48f1ffd1990e8794 Mon Sep 17 00:00:00 2001 From: ilotterytea Date: Sun, 5 Jan 2025 00:14:45 +0500 Subject: feat: established communication between game and server --- .../src/kz/ilotterytea/maxon/PlayerConnection.java | 66 ++++++++++++++++++++++ 1 file changed, 66 insertions(+) create mode 100644 server/src/kz/ilotterytea/maxon/PlayerConnection.java (limited to 'server/src/kz/ilotterytea/maxon/PlayerConnection.java') diff --git a/server/src/kz/ilotterytea/maxon/PlayerConnection.java b/server/src/kz/ilotterytea/maxon/PlayerConnection.java new file mode 100644 index 0000000..79b46d0 --- /dev/null +++ b/server/src/kz/ilotterytea/maxon/PlayerConnection.java @@ -0,0 +1,66 @@ +package kz.ilotterytea.maxon; + +import kz.ilotterytea.maxon.shared.Identity; +import org.java_websocket.WebSocket; + +import java.io.ByteArrayOutputStream; +import java.io.IOException; +import java.io.ObjectOutputStream; +import java.sql.Timestamp; + +public class PlayerConnection { + private static int TOTAL_CONNECTION_IDS = 0; + + private final int id; + private final WebSocket connection; + private Identity identity; + + private final Timestamp connectedTimestamp; + + public PlayerConnection(WebSocket connection) { + this.connection = connection; + this.connectedTimestamp = new Timestamp(System.currentTimeMillis()); + + this.id = TOTAL_CONNECTION_IDS; + TOTAL_CONNECTION_IDS++; + } + + public int getId() { + return id; + } + + public WebSocket getConnection() { + return connection; + } + + public Identity getIdentity() { + return identity; + } + + public void setIdentity(Identity identity) { + this.identity = identity; + } + + public Timestamp getConnectedTimestamp() { + return connectedTimestamp; + } + + public void send(Object object) { + ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream(); + + try (ObjectOutputStream objectOutputStream = new ObjectOutputStream(byteArrayOutputStream)) { + objectOutputStream.writeObject(object); + } catch (IOException ignored) { + + } + + this.connection.send(byteArrayOutputStream.toByteArray()); + } + + @Override + public String toString() { + return "PlayerConnection{" + + "id=" + id + + '}'; + } +} -- cgit v1.2.3