diff options
| author | ilotterytea <iltsu@alright.party> | 2025-01-21 03:17:15 +0500 |
|---|---|---|
| committer | ilotterytea <iltsu@alright.party> | 2025-01-21 03:17:15 +0500 |
| commit | 1ecd8d1d527b6b66b4746e44023b6ab3cb2ca4cc (patch) | |
| tree | 44a766670059ef3beb1da048844fc7693418bcf8 /server/src | |
| parent | 4e524d08c5b427cabc1a6e452347c09c963eb86c (diff) | |
feat: using other serializer and gdx-websocket (because the old one doesn't support gwt)
Diffstat (limited to 'server/src')
3 files changed, 21 insertions, 17 deletions
diff --git a/server/src/main/java/kz/ilotterytea/frogartha/server/FrogarthaServer.java b/server/src/main/java/kz/ilotterytea/frogartha/server/FrogarthaServer.java index c3fcd77..1dbb4bd 100644 --- a/server/src/main/java/kz/ilotterytea/frogartha/server/FrogarthaServer.java +++ b/server/src/main/java/kz/ilotterytea/frogartha/server/FrogarthaServer.java @@ -1,7 +1,9 @@ package kz.ilotterytea.frogartha.server; +import com.github.czyzby.websocket.serialization.impl.ManualSerializer; import kz.ilotterytea.frogartha.domain.Identity; import kz.ilotterytea.frogartha.exceptions.PlayerKickException; +import kz.ilotterytea.frogartha.utils.SerializerUtils; import org.java_websocket.WebSocket; import org.java_websocket.framing.CloseFrame; import org.java_websocket.handshake.ClientHandshake; @@ -9,8 +11,6 @@ import org.java_websocket.server.WebSocketServer; import org.slf4j.Logger; import org.slf4j.LoggerFactory; -import java.io.ByteArrayInputStream; -import java.io.ObjectInputStream; import java.net.InetSocketAddress; import java.nio.ByteBuffer; import java.util.ArrayList; @@ -22,10 +22,15 @@ public class FrogarthaServer extends WebSocketServer { private final Logger log; private final ArrayList<PlayerConnection> players; + private final ManualSerializer serializer; + private FrogarthaServer() { super(new InetSocketAddress(20015)); this.log = LoggerFactory.getLogger(FrogarthaServer.class); this.players = new ArrayList<>(); + + this.serializer = new ManualSerializer(); + SerializerUtils.registerTypes(serializer); } @Override @@ -66,10 +71,11 @@ public class FrogarthaServer extends WebSocketServer { PlayerConnection player = optionalPlayer.get(); try { - // Deserializing the object - ByteArrayInputStream bais = new ByteArrayInputStream(message.array()); - ObjectInputStream ois = new ObjectInputStream(bais); - Object obj = ois.readObject(); + Object obj = serializer.deserialize(message.array()); + + if (obj == null) { + throw PlayerKickException.internalServerError(); + } if (obj instanceof Identity) ServerHandlers.handleIdentity(player, (Identity) obj); else throw PlayerKickException.internalServerError(); @@ -108,4 +114,8 @@ public class FrogarthaServer extends WebSocketServer { public ArrayList<PlayerConnection> getPlayers() { return players; } + + public ManualSerializer getSerializer() { + return serializer; + } } diff --git a/server/src/main/java/kz/ilotterytea/frogartha/server/PlayerConnection.java b/server/src/main/java/kz/ilotterytea/frogartha/server/PlayerConnection.java index c62dce5..d9326a6 100644 --- a/server/src/main/java/kz/ilotterytea/frogartha/server/PlayerConnection.java +++ b/server/src/main/java/kz/ilotterytea/frogartha/server/PlayerConnection.java @@ -3,9 +3,6 @@ package kz.ilotterytea.frogartha.server; import kz.ilotterytea.frogartha.domain.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 { @@ -27,15 +24,12 @@ public class PlayerConnection { } public void send(Object object) { - ByteArrayOutputStream baos = new ByteArrayOutputStream(); - - try (ObjectOutputStream oos = new ObjectOutputStream(baos)) { - oos.writeObject(object); - } catch (IOException ignored) { + FrogarthaServer server = FrogarthaServer.getInstance(); + try { + connection.send(server.getSerializer().serialize(object)); + } catch (Exception ignored) { } - - this.connection.send(baos.toByteArray()); } public int getId() { diff --git a/server/src/main/java/kz/ilotterytea/frogartha/server/ServerHandlers.java b/server/src/main/java/kz/ilotterytea/frogartha/server/ServerHandlers.java index 7b4a868..7ad2f25 100644 --- a/server/src/main/java/kz/ilotterytea/frogartha/server/ServerHandlers.java +++ b/server/src/main/java/kz/ilotterytea/frogartha/server/ServerHandlers.java @@ -20,7 +20,7 @@ public class ServerHandlers { } player.setIdentity(identity); - player.send(new Acknowledge(identity)); + player.send(new Acknowledge(Acknowledge.AcknowledgeCode.IDENTIFIED)); log.debug("Successfully identified {} for {}", identity, player); } } |
