diff options
Diffstat (limited to 'server/src/kz/ilotterytea/maxon/PlayerConnection.java')
| -rw-r--r-- | server/src/kz/ilotterytea/maxon/PlayerConnection.java | 66 |
1 files changed, 66 insertions, 0 deletions
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 + + '}'; + } +} |
