summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorilotterytea <iltsu@alright.party>2025-01-21 03:17:15 +0500
committerilotterytea <iltsu@alright.party>2025-01-21 03:17:15 +0500
commit1ecd8d1d527b6b66b4746e44023b6ab3cb2ca4cc (patch)
tree44a766670059ef3beb1da048844fc7693418bcf8
parent4e524d08c5b427cabc1a6e452347c09c963eb86c (diff)
feat: using other serializer and gdx-websocket (because the old one doesn't support gwt)
-rw-r--r--core/build.gradle3
-rw-r--r--core/src/main/java/kz/ilotterytea/frogartha/sessions/SessionClient.java75
-rw-r--r--core/src/main/java/kz/ilotterytea/frogartha/sessions/SessionHandlers.java3
-rw-r--r--gradle.properties2
-rw-r--r--html/build.gradle19
-rw-r--r--html/src/main/java/kz/ilotterytea/frogartha/GdxDefinition.gwt.xml60
-rw-r--r--html/src/main/java/kz/ilotterytea/frogartha/gwt/GwtLauncher.java2
-rw-r--r--lwjgl3/build.gradle82
-rw-r--r--lwjgl3/src/main/java/kz/ilotterytea/frogartha/lwjgl3/Lwjgl3Launcher.java2
-rw-r--r--server/src/main/java/kz/ilotterytea/frogartha/server/FrogarthaServer.java22
-rw-r--r--server/src/main/java/kz/ilotterytea/frogartha/server/PlayerConnection.java14
-rw-r--r--server/src/main/java/kz/ilotterytea/frogartha/server/ServerHandlers.java2
-rw-r--r--shared/build.gradle3
-rw-r--r--shared/src/main/java/kz/ilotterytea/frogartha/domain/Identity.java22
-rw-r--r--shared/src/main/java/kz/ilotterytea/frogartha/domain/server/Acknowledge.java36
-rw-r--r--shared/src/main/java/kz/ilotterytea/frogartha/exceptions/PlayerKickException.java20
-rw-r--r--shared/src/main/java/kz/ilotterytea/frogartha/utils/SerializerUtils.java14
17 files changed, 240 insertions, 141 deletions
diff --git a/core/build.gradle b/core/build.gradle
index eac5714..a731b5d 100644
--- a/core/build.gradle
+++ b/core/build.gradle
@@ -2,9 +2,8 @@
eclipse.project.name = appName + '-core'
dependencies {
- api "com.badlogicgames.gdx:gdx:$gdxVersion"
api "com.github.mgsx-dev.gdx-gltf:gltf:$gdxGltfVersion"
- api "org.java-websocket:Java-WebSocket:$javaWebsocketVersion"
+ api "com.github.MrStahlfelge.gdx-websockets:core:$gdxWsVersion"
api "ch.qos.logback:logback-classic:$logbackVersion"
api project(':shared')
diff --git a/core/src/main/java/kz/ilotterytea/frogartha/sessions/SessionClient.java b/core/src/main/java/kz/ilotterytea/frogartha/sessions/SessionClient.java
index 555c287..67afd69 100644
--- a/core/src/main/java/kz/ilotterytea/frogartha/sessions/SessionClient.java
+++ b/core/src/main/java/kz/ilotterytea/frogartha/sessions/SessionClient.java
@@ -1,49 +1,63 @@
package kz.ilotterytea.frogartha.sessions;
+import com.github.czyzby.websocket.WebSocket;
+import com.github.czyzby.websocket.WebSocketListener;
+import com.github.czyzby.websocket.WebSockets;
+import com.github.czyzby.websocket.serialization.impl.ManualSerializer;
import kz.ilotterytea.frogartha.FrogarthaConstants;
import kz.ilotterytea.frogartha.FrogarthaGame;
import kz.ilotterytea.frogartha.domain.Identity;
import kz.ilotterytea.frogartha.domain.server.Acknowledge;
import kz.ilotterytea.frogartha.exceptions.PlayerKickException;
-import org.java_websocket.client.WebSocketClient;
-import org.java_websocket.handshake.ServerHandshake;
+import kz.ilotterytea.frogartha.utils.SerializerUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
-import java.io.ByteArrayInputStream;
-import java.io.ByteArrayOutputStream;
-import java.io.ObjectInputStream;
-import java.io.ObjectOutputStream;
-import java.net.URI;
-import java.nio.ByteBuffer;
-
-public class SessionClient extends WebSocketClient {
+public class SessionClient implements WebSocketListener {
private final Logger log;
private final FrogarthaGame game;
+ private final WebSocket connection;
+ private final ManualSerializer serializer;
public SessionClient() {
- super(URI.create(FrogarthaConstants.URLS.SESSION_WSS));
+ this.connection = WebSockets.newSocket(FrogarthaConstants.URLS.SESSION_WSS);
+ this.connection.addListener(this);
+
+ this.serializer = new ManualSerializer();
+ SerializerUtils.registerTypes(serializer);
+ connection.setSerializer(serializer);
+
this.log = LoggerFactory.getLogger(SessionClient.class);
this.game = FrogarthaGame.getInstance();
}
@Override
- public void onOpen(ServerHandshake handshakedata) {
+ public boolean onOpen(WebSocket webSocket) {
log.info("Connected");
updateIdentity();
+ return true;
+ }
+
+ @Override
+ public boolean onClose(WebSocket webSocket, int closeCode, String reason) {
+ log.info("Connection closed! Reason: {} {}", closeCode, reason);
+ game.getIdentityClient().setAuthorized(false);
+ return true;
}
@Override
- public void onMessage(String message) {
+ public boolean onMessage(WebSocket webSocket, String packet) {
+ return false;
}
@Override
- public void onMessage(ByteBuffer bytes) {
+ public boolean onMessage(WebSocket webSocket, byte[] packet) {
try {
- // Deserialize the object
- ByteArrayInputStream bais = new ByteArrayInputStream(bytes.array());
- ObjectInputStream ois = new ObjectInputStream(bais);
- Object obj = ois.readObject();
+ Object obj = serializer.deserialize(packet);
+
+ if (obj == null) {
+ throw new RuntimeException("Deserialized packet is null");
+ }
if (obj instanceof Acknowledge) SessionHandlers.handleAcknowledge((Acknowledge) obj);
else if (obj instanceof PlayerKickException) throw (PlayerKickException) obj;
@@ -52,30 +66,31 @@ public class SessionClient extends WebSocketClient {
} catch (Exception e) {
log.error("An exception was thrown while processing message", e);
}
+ return true;
}
@Override
- public void onClose(int code, String reason, boolean remote) {
- log.info("Connection closed! Reason: {} {}", code, reason);
- game.getIdentityClient().setAuthorized(false);
- }
-
- @Override
- public void onError(Exception ex) {
- log.error("Failed to connect", ex);
+ public boolean onError(WebSocket webSocket, Throwable error) {
+ log.error("An exception was thrown on session connection", error);
+ return true;
}
public void send(Object object) {
try {
- ByteArrayOutputStream baos = new ByteArrayOutputStream();
- ObjectOutputStream oos = new ObjectOutputStream(baos);
- oos.writeObject(object);
- send(baos.toByteArray());
+ connection.send(serializer.serialize(object));
} catch (Exception e) {
log.error("Failed to serialize and send an object", e);
}
}
+ public void connect() {
+ connection.connect();
+ }
+
+ public void close() {
+ connection.close();
+ }
+
public void updateIdentity() {
send(new Identity(game.getIdentityClient().getUsername()));
}
diff --git a/core/src/main/java/kz/ilotterytea/frogartha/sessions/SessionHandlers.java b/core/src/main/java/kz/ilotterytea/frogartha/sessions/SessionHandlers.java
index 0a298dc..cb6457d 100644
--- a/core/src/main/java/kz/ilotterytea/frogartha/sessions/SessionHandlers.java
+++ b/core/src/main/java/kz/ilotterytea/frogartha/sessions/SessionHandlers.java
@@ -1,7 +1,6 @@
package kz.ilotterytea.frogartha.sessions;
import kz.ilotterytea.frogartha.FrogarthaGame;
-import kz.ilotterytea.frogartha.domain.Identity;
import kz.ilotterytea.frogartha.domain.server.Acknowledge;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
@@ -14,7 +13,7 @@ public class SessionHandlers {
public static void handleAcknowledge(Acknowledge acknowledge) {
log.info("The server was acknowledged: {}", acknowledge);
- if (acknowledge.getPayload() instanceof Identity) {
+ if (acknowledge.getCode() == Acknowledge.AcknowledgeCode.IDENTIFIED) {
game.getIdentityClient().setAuthorized(true);
}
}
diff --git a/gradle.properties b/gradle.properties
index 015fb0f..5e93e6d 100644
--- a/gradle.properties
+++ b/gradle.properties
@@ -7,6 +7,8 @@ gwtFrameworkVersion=2.11.0
gwtPluginVersion=1.1.29
enableGraalNative=false
gdxVersion=1.13.1
+gdxWsVersion=1.9.10.3
+gdxWsLibVersion=1.9.1.9.6
javaWebsocketVersion=1.6.0
logbackVersion=1.5.15
projectVersion=1.0.0
diff --git a/html/build.gradle b/html/build.gradle
index 2036b11..89d07bd 100644
--- a/html/build.gradle
+++ b/html/build.gradle
@@ -1,4 +1,3 @@
-
buildscript {
repositories {
mavenCentral()
@@ -14,7 +13,8 @@ apply plugin: "war"
apply plugin: "org.gretty"
gwt {
- gwtVersion = "$gwtFrameworkVersion" // Should match the version used for building the GWT backend. See gradle.properties.
+ gwtVersion = "$gwtFrameworkVersion"
+ // Should match the version used for building the GWT backend. See gradle.properties.
maxHeapSize = '1G' // Default 256m is not enough for the GWT compiler. GWT is HUNGRY.
minHeapSize = '1G'
@@ -36,6 +36,9 @@ dependencies {
implementation "com.badlogicgames.gdx:gdx-backend-gwt:$gdxVersion:sources"
implementation "com.badlogicgames.gdx:gdx:$gdxVersion:sources"
implementation "com.github.mgsx-dev.gdx-gltf:gltf:$gdxGltfVersion:sources"
+ implementation "com.github.MrStahlfelge.gdx-websockets:core:$gdxWsVersion:sources"
+ implementation "com.github.MrStahlfelge.gdx-websockets:html:$gdxWsVersion"
+ implementation "com.github.MrStahlfelge.gdx-websockets:html:$gdxWsVersion:sources"
implementation project(':core')
}
@@ -49,7 +52,7 @@ gretty.resourceBase = "${project.layout.buildDirectory.asFile.get().absolutePath
gretty.contextPath = "/"
gretty.portPropertiesFileName = "TEMP_PORTS.properties"
-task startHttpServer (dependsOn: [draftCompileGwt]) {
+task startHttpServer(dependsOn: [draftCompileGwt]) {
doFirst {
copy {
from "webapp"
@@ -99,7 +102,7 @@ task dist(dependsOn: [clean, compileGwt]) {
file(outputPath).mkdirs()
copy {
- from("build/gwt/out"){
+ from("build/gwt/out") {
exclude '**/*.symbolMap' // Not used by a dist, and these can be large.
}
into outputPath
@@ -110,7 +113,7 @@ task dist(dependsOn: [clean, compileGwt]) {
exclude 'refresh.png' // We don't need this button; this saves some bytes.
}
into outputPath
- }
+ }
copy {
from("webapp") {
// These next two lines take the index.html page and remove the superdev refresh button.
@@ -121,7 +124,7 @@ task dist(dependsOn: [clean, compileGwt]) {
// either remove or comment out only the "filter" line above this.
}
into outputPath
- }
+ }
copy {
from "war"
into outputPath
@@ -137,7 +140,7 @@ task addSource {
}
}
-task distZip(type: Zip, dependsOn: dist){
+task distZip(type: Zip, dependsOn: dist) {
//// This uses the output of the dist task, which removes the superdev button from index.html .
from(outputPath)
archiveVersion = projectVersion
@@ -152,6 +155,6 @@ tasks.checkGwt.dependsOn(addSource)
java.sourceCompatibility = JavaVersion.VERSION_11
java.targetCompatibility = JavaVersion.VERSION_11
-sourceSets.main.java.srcDirs = [ "src/main/java/" ]
+sourceSets.main.java.srcDirs = ["src/main/java/"]
eclipse.project.name = appName + "-html"
diff --git a/html/src/main/java/kz/ilotterytea/frogartha/GdxDefinition.gwt.xml b/html/src/main/java/kz/ilotterytea/frogartha/GdxDefinition.gwt.xml
index 3f287d1..266f4a8 100644
--- a/html/src/main/java/kz/ilotterytea/frogartha/GdxDefinition.gwt.xml
+++ b/html/src/main/java/kz/ilotterytea/frogartha/GdxDefinition.gwt.xml
@@ -1,35 +1,39 @@
<?xml version="1.0" encoding="UTF-8"?>
-<!DOCTYPE module PUBLIC "-//Google Inc.//DTD Google Web Toolkit 2.11.0//EN" "https://www.gwtproject.org/doctype/2.11.0/gwt-module.dtd">
+<!DOCTYPE module PUBLIC "-//Google Inc.//DTD Google Web Toolkit 2.11.0//EN"
+ "https://www.gwtproject.org/doctype/2.11.0/gwt-module.dtd">
<module rename-to="html">
- <!-- Paths to source are relative to this file and separated by slashes ('/'). -->
- <source path="" />
+ <!-- Paths to source are relative to this file and separated by slashes ('/'). -->
+ <source path=""/>
- <!-- "Inherits" lines are how GWT knows where to look for code and configuration in other projects or libraries. -->
- <inherits name="com.badlogic.gdx.backends.gdx_backends_gwt" />
- <inherits name="GLTF" />
- <inherits name="kz.ilotterytea.frogartha.FrogarthaGame" />
- <inherits name="kz.ilotterytea.frogartha.Shared" />
+ <!-- "Inherits" lines are how GWT knows where to look for code and configuration in other projects or libraries. -->
+ <inherits name="com.badlogic.gdx.backends.gdx_backends_gwt"/>
+ <inherits name="GLTF"/>
+ <inherits name="kz.ilotterytea.frogartha.FrogarthaGame"/>
+ <inherits name="kz.ilotterytea.frogartha.Shared"/>
+ <inherits name='com.github.czyzby.websocket.GdxWebSocket'/>
+ <inherits name='com.github.czyzby.websocket.GdxWebSocketGwt'/>
+ <inherits name='com.github.czyzby.websocket.GdxWebSocketSerialization'/>
- <!-- You must change this if you rename packages later, or rename GwtLauncher. -->
- <entry-point class="kz.ilotterytea.frogartha.gwt.GwtLauncher" />
+ <!-- You must change this if you rename packages later, or rename GwtLauncher. -->
+ <entry-point class="kz.ilotterytea.frogartha.gwt.GwtLauncher"/>
- <!-- Reflection includes may be needed for your code or library code. Each value is separated by periods ('.'). -->
- <!-- You can include a full package by not including the name of a type at the end. -->
- <!-- This is a feature of libGDX, so these lines go after the above "inherits" that brings in libGDX. -->
- <!-- <extend-configuration-property name="gdx.reflect.include" value="fully.qualified.TypeName" /> -->
+ <!-- Reflection includes may be needed for your code or library code. Each value is separated by periods ('.'). -->
+ <!-- You can include a full package by not including the name of a type at the end. -->
+ <!-- This is a feature of libGDX, so these lines go after the above "inherits" that brings in libGDX. -->
+ <!-- <extend-configuration-property name="gdx.reflect.include" value="fully.qualified.TypeName" /> -->
- <!-- Rarely, projects may need to include files but do not have access to the complete assets. -->
- <!-- This happens for libraries and shared projects, typically, and the configuration goes in that project. -->
- <!-- The value is a path, separated by forward slashes, where the root is your html project's resources root. -->
- <!-- You can include individual files like this, and access them with Gdx.files.classpath("path/to/file.png") : -->
- <!-- This is also a feature of libGDX, so these lines go after the above "inherits" that brings in libGDX. -->
- <!-- <extend-configuration-property name="gdx.files.classpath" value="path/to/file.png" /> -->
+ <!-- Rarely, projects may need to include files but do not have access to the complete assets. -->
+ <!-- This happens for libraries and shared projects, typically, and the configuration goes in that project. -->
+ <!-- The value is a path, separated by forward slashes, where the root is your html project's resources root. -->
+ <!-- You can include individual files like this, and access them with Gdx.files.classpath("path/to/file.png") : -->
+ <!-- This is also a feature of libGDX, so these lines go after the above "inherits" that brings in libGDX. -->
+ <!-- <extend-configuration-property name="gdx.files.classpath" value="path/to/file.png" /> -->
- <!-- You usually won't need to make changes to the rest of this. -->
- <set-configuration-property name="gdx.assetpath" value="../assets" />
- <set-configuration-property name="xsiframe.failIfScriptTag" value="FALSE"/>
- <!-- These two lines reduce the work GWT has to do during compilation and also shrink output size. -->
- <set-property name="user.agent" value="gecko1_8, safari"/>
- <collapse-property name="user.agent" values="*" />
- <!-- Remove the "user.agent" lines above if you encounter issues with Safari or other Gecko browsers. -->
-</module> \ No newline at end of file
+ <!-- You usually won't need to make changes to the rest of this. -->
+ <set-configuration-property name="gdx.assetpath" value="../assets"/>
+ <set-configuration-property name="xsiframe.failIfScriptTag" value="FALSE"/>
+ <!-- These two lines reduce the work GWT has to do during compilation and also shrink output size. -->
+ <set-property name="user.agent" value="gecko1_8, safari"/>
+ <collapse-property name="user.agent" values="*"/>
+ <!-- Remove the "user.agent" lines above if you encounter issues with Safari or other Gecko browsers. -->
+</module>
diff --git a/html/src/main/java/kz/ilotterytea/frogartha/gwt/GwtLauncher.java b/html/src/main/java/kz/ilotterytea/frogartha/gwt/GwtLauncher.java
index a4e7ea3..bafa596 100644
--- a/html/src/main/java/kz/ilotterytea/frogartha/gwt/GwtLauncher.java
+++ b/html/src/main/java/kz/ilotterytea/frogartha/gwt/GwtLauncher.java
@@ -3,6 +3,7 @@ package kz.ilotterytea.frogartha.gwt;
import com.badlogic.gdx.ApplicationListener;
import com.badlogic.gdx.backends.gwt.GwtApplication;
import com.badlogic.gdx.backends.gwt.GwtApplicationConfiguration;
+import com.github.czyzby.websocket.GwtWebSockets;
import kz.ilotterytea.frogartha.FrogarthaGame;
/**
@@ -23,6 +24,7 @@ public class GwtLauncher extends GwtApplication {
@Override
public ApplicationListener createApplicationListener() {
+ GwtWebSockets.initiate();
return FrogarthaGame.getInstance();
}
}
diff --git a/lwjgl3/build.gradle b/lwjgl3/build.gradle
index e2986c6..ac9b29f 100644
--- a/lwjgl3/build.gradle
+++ b/lwjgl3/build.gradle
@@ -1,11 +1,10 @@
-
buildscript {
repositories {
gradlePluginPortal()
}
dependencies {
classpath "io.github.fourlastor:construo:1.5.1"
- if(enableGraalNative == 'true') {
+ if (enableGraalNative == 'true') {
classpath "org.graalvm.buildtools.native:org.graalvm.buildtools.native.gradle.plugin:0.9.28"
}
}
@@ -18,24 +17,25 @@ apply plugin: 'io.github.fourlastor.construo'
import io.github.fourlastor.construo.Target
-sourceSets.main.resources.srcDirs += [ rootProject.file('assets').path ]
+sourceSets.main.resources.srcDirs += [rootProject.file('assets').path]
mainClassName = 'kz.ilotterytea.frogartha.lwjgl3.Lwjgl3Launcher'
application.setMainClass(mainClassName)
eclipse.project.name = appName + '-lwjgl3'
java.sourceCompatibility = 11
java.targetCompatibility = 11
if (JavaVersion.current().isJava9Compatible()) {
- compileJava.options.release.set(11)
+ compileJava.options.release.set(11)
}
dependencies {
implementation "com.badlogicgames.gdx:gdx-backend-lwjgl3:$gdxVersion"
implementation "com.badlogicgames.gdx:gdx-platform:$gdxVersion:natives-desktop"
+ implementation "com.github.MrStahlfelge.gdx-websockets:common:$gdxWsVersion"
implementation project(':core')
- if(enableGraalNative == 'true') {
+ if (enableGraalNative == 'true') {
implementation "io.github.berstanio:gdx-svmhelper-backend-lwjgl3:$graalHelperVersion"
- }
+ }
}
@@ -72,41 +72,41 @@ jar {
}
construo {
- // name of the executable
- name.set(appName)
- // human-readable name, used for example in the `.app` name for macOS
- humanName.set(appName)
- // Optional, defaults to project version property
- version.set("$projectVersion")
-
- targets.configure {
- create("linuxX64", Target.Linux) {
- architecture.set(Target.Architecture.X86_64)
- jdkUrl.set("https://github.com/adoptium/temurin17-binaries/releases/download/jdk-17.0.12%2B7/OpenJDK17U-jdk_x64_linux_hotspot_17.0.12_7.tar.gz")
- }
- create("macM1", Target.MacOs) {
- architecture.set(Target.Architecture.AARCH64)
- jdkUrl.set("https://github.com/adoptium/temurin17-binaries/releases/download/jdk-17.0.12%2B7/OpenJDK17U-jdk_aarch64_mac_hotspot_17.0.12_7.tar.gz")
- // macOS needs an identifier
- identifier.set("kz.ilotterytea.frogartha." + appName)
- // Optional: icon for macOS
- macIcon.set(project.file("icons/logo.icns"))
- }
- create("macX64", Target.MacOs) {
- architecture.set(Target.Architecture.X86_64)
- jdkUrl.set("https://github.com/adoptium/temurin17-binaries/releases/download/jdk-17.0.12%2B7/OpenJDK17U-jdk_x64_mac_hotspot_17.0.12_7.tar.gz")
- // macOS needs an identifier
- identifier.set("kz.ilotterytea.frogartha." + appName)
- // Optional: icon for macOS
- macIcon.set(project.file("icons/logo.icns"))
- }
- create("winX64", Target.Windows) {
- architecture.set(Target.Architecture.X86_64)
- jdkUrl.set("https://github.com/adoptium/temurin17-binaries/releases/download/jdk-17.0.12%2B7/OpenJDK17U-jdk_x64_windows_hotspot_17.0.12_7.zip")
- // Uncomment the next line to show a console when the game runs, to print messages.
- //useConsole.set(true)
- }
+ // name of the executable
+ name.set(appName)
+ // human-readable name, used for example in the `.app` name for macOS
+ humanName.set(appName)
+ // Optional, defaults to project version property
+ version.set("$projectVersion")
+
+ targets.configure {
+ create("linuxX64", Target.Linux) {
+ architecture.set(Target.Architecture.X86_64)
+ jdkUrl.set("https://github.com/adoptium/temurin17-binaries/releases/download/jdk-17.0.12%2B7/OpenJDK17U-jdk_x64_linux_hotspot_17.0.12_7.tar.gz")
+ }
+ create("macM1", Target.MacOs) {
+ architecture.set(Target.Architecture.AARCH64)
+ jdkUrl.set("https://github.com/adoptium/temurin17-binaries/releases/download/jdk-17.0.12%2B7/OpenJDK17U-jdk_aarch64_mac_hotspot_17.0.12_7.tar.gz")
+ // macOS needs an identifier
+ identifier.set("kz.ilotterytea.frogartha." + appName)
+ // Optional: icon for macOS
+ macIcon.set(project.file("icons/logo.icns"))
+ }
+ create("macX64", Target.MacOs) {
+ architecture.set(Target.Architecture.X86_64)
+ jdkUrl.set("https://github.com/adoptium/temurin17-binaries/releases/download/jdk-17.0.12%2B7/OpenJDK17U-jdk_x64_mac_hotspot_17.0.12_7.tar.gz")
+ // macOS needs an identifier
+ identifier.set("kz.ilotterytea.frogartha." + appName)
+ // Optional: icon for macOS
+ macIcon.set(project.file("icons/logo.icns"))
}
+ create("winX64", Target.Windows) {
+ architecture.set(Target.Architecture.X86_64)
+ jdkUrl.set("https://github.com/adoptium/temurin17-binaries/releases/download/jdk-17.0.12%2B7/OpenJDK17U-jdk_x64_windows_hotspot_17.0.12_7.zip")
+ // Uncomment the next line to show a console when the game runs, to print messages.
+ //useConsole.set(true)
+ }
+ }
}
// Equivalent to the jar task; here for compatibility with gdx-setup.
@@ -131,6 +131,6 @@ distributions {
startScripts.dependsOn(':lwjgl3:jar')
startScripts.classpath = project.tasks.jar.outputs.files
-if(enableGraalNative == 'true') {
+if (enableGraalNative == 'true') {
apply from: file("nativeimage.gradle")
}
diff --git a/lwjgl3/src/main/java/kz/ilotterytea/frogartha/lwjgl3/Lwjgl3Launcher.java b/lwjgl3/src/main/java/kz/ilotterytea/frogartha/lwjgl3/Lwjgl3Launcher.java
index 4767186..86a6985 100644
--- a/lwjgl3/src/main/java/kz/ilotterytea/frogartha/lwjgl3/Lwjgl3Launcher.java
+++ b/lwjgl3/src/main/java/kz/ilotterytea/frogartha/lwjgl3/Lwjgl3Launcher.java
@@ -2,6 +2,7 @@ package kz.ilotterytea.frogartha.lwjgl3;
import com.badlogic.gdx.backends.lwjgl3.Lwjgl3Application;
import com.badlogic.gdx.backends.lwjgl3.Lwjgl3ApplicationConfiguration;
+import com.github.czyzby.websocket.CommonWebSockets;
import kz.ilotterytea.frogartha.FrogarthaGame;
/**
@@ -10,6 +11,7 @@ import kz.ilotterytea.frogartha.FrogarthaGame;
public class Lwjgl3Launcher {
public static void main(String[] args) {
if (StartupHelper.startNewJvmIfRequired()) return; // This handles macOS support and helps on Windows.
+ CommonWebSockets.initiate();
new Lwjgl3Application(FrogarthaGame.getInstance(), getDefaultConfiguration());
}
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);
}
}
diff --git a/shared/build.gradle b/shared/build.gradle
index 1eafc3b..d2344a1 100644
--- a/shared/build.gradle
+++ b/shared/build.gradle
@@ -1,5 +1,6 @@
eclipse.project.name = appName + '-shared'
dependencies {
-
+ api "com.badlogicgames.gdx:gdx:$gdxVersion"
+ api "com.github.czyzby:gdx-websocket-serialization:$gdxWsLibVersion"
}
diff --git a/shared/src/main/java/kz/ilotterytea/frogartha/domain/Identity.java b/shared/src/main/java/kz/ilotterytea/frogartha/domain/Identity.java
index 9a72268..03c0ef4 100644
--- a/shared/src/main/java/kz/ilotterytea/frogartha/domain/Identity.java
+++ b/shared/src/main/java/kz/ilotterytea/frogartha/domain/Identity.java
@@ -1,9 +1,15 @@
package kz.ilotterytea.frogartha.domain;
-import java.io.Serializable;
+import com.github.czyzby.websocket.serialization.SerializationException;
+import com.github.czyzby.websocket.serialization.Transferable;
+import com.github.czyzby.websocket.serialization.impl.Deserializer;
+import com.github.czyzby.websocket.serialization.impl.Serializer;
-public class Identity implements Serializable {
- private final String username;
+public class Identity implements Transferable<Identity> {
+ private String username;
+
+ public Identity() {
+ }
public Identity(String username) {
this.username = username;
@@ -27,4 +33,14 @@ public class Identity implements Serializable {
"username='" + username + '\'' +
'}';
}
+
+ @Override
+ public void serialize(Serializer serializer) throws SerializationException {
+ serializer.serializeString(username);
+ }
+
+ @Override
+ public Identity deserialize(Deserializer deserializer) throws SerializationException {
+ return new Identity(deserializer.deserializeString());
+ }
}
diff --git a/shared/src/main/java/kz/ilotterytea/frogartha/domain/server/Acknowledge.java b/shared/src/main/java/kz/ilotterytea/frogartha/domain/server/Acknowledge.java
index f82c1c9..8ca0f66 100644
--- a/shared/src/main/java/kz/ilotterytea/frogartha/domain/server/Acknowledge.java
+++ b/shared/src/main/java/kz/ilotterytea/frogartha/domain/server/Acknowledge.java
@@ -1,22 +1,42 @@
package kz.ilotterytea.frogartha.domain.server;
-import java.io.Serializable;
+import com.github.czyzby.websocket.serialization.SerializationException;
+import com.github.czyzby.websocket.serialization.Transferable;
+import com.github.czyzby.websocket.serialization.impl.Deserializer;
+import com.github.czyzby.websocket.serialization.impl.Serializer;
-public class Acknowledge implements Serializable {
- private final Object payload;
+public class Acknowledge implements Transferable<Acknowledge> {
+ public enum AcknowledgeCode {
+ IDENTIFIED
+ }
+
+ private AcknowledgeCode code;
- public Acknowledge(Object payload) {
- this.payload = payload;
+ public Acknowledge() {
}
- public Object getPayload() {
- return payload;
+ public Acknowledge(AcknowledgeCode code) {
+ this.code = code;
+ }
+
+ public AcknowledgeCode getCode() {
+ return code;
}
@Override
public String toString() {
return "Acknowledge{" +
- "payload=" + payload +
+ "code=" + code.toString() +
'}';
}
+
+ @Override
+ public void serialize(Serializer serializer) throws SerializationException {
+ serializer.serializeInt(code.ordinal());
+ }
+
+ @Override
+ public Acknowledge deserialize(Deserializer deserializer) throws SerializationException {
+ return new Acknowledge(AcknowledgeCode.values()[deserializer.deserializeInt()]);
+ }
}
diff --git a/shared/src/main/java/kz/ilotterytea/frogartha/exceptions/PlayerKickException.java b/shared/src/main/java/kz/ilotterytea/frogartha/exceptions/PlayerKickException.java
index a6d8c0a..4eaf389 100644
--- a/shared/src/main/java/kz/ilotterytea/frogartha/exceptions/PlayerKickException.java
+++ b/shared/src/main/java/kz/ilotterytea/frogartha/exceptions/PlayerKickException.java
@@ -1,6 +1,14 @@
package kz.ilotterytea.frogartha.exceptions;
-public class PlayerKickException extends RuntimeException {
+import com.github.czyzby.websocket.serialization.SerializationException;
+import com.github.czyzby.websocket.serialization.Transferable;
+import com.github.czyzby.websocket.serialization.impl.Deserializer;
+import com.github.czyzby.websocket.serialization.impl.Serializer;
+
+public class PlayerKickException extends RuntimeException implements Transferable<PlayerKickException> {
+ public PlayerKickException() {
+ }
+
private PlayerKickException(String message) {
super(message);
}
@@ -12,4 +20,14 @@ public class PlayerKickException extends RuntimeException {
public static PlayerKickException internalServerError() {
return new PlayerKickException("Internal Server Error");
}
+
+ @Override
+ public void serialize(Serializer serializer) throws SerializationException {
+ serializer.serializeString(getMessage());
+ }
+
+ @Override
+ public PlayerKickException deserialize(Deserializer deserializer) throws SerializationException {
+ return new PlayerKickException(deserializer.deserializeString());
+ }
}
diff --git a/shared/src/main/java/kz/ilotterytea/frogartha/utils/SerializerUtils.java b/shared/src/main/java/kz/ilotterytea/frogartha/utils/SerializerUtils.java
new file mode 100644
index 0000000..bf0c3f3
--- /dev/null
+++ b/shared/src/main/java/kz/ilotterytea/frogartha/utils/SerializerUtils.java
@@ -0,0 +1,14 @@
+package kz.ilotterytea.frogartha.utils;
+
+import com.github.czyzby.websocket.serialization.impl.ManualSerializer;
+import kz.ilotterytea.frogartha.domain.Identity;
+import kz.ilotterytea.frogartha.domain.server.Acknowledge;
+import kz.ilotterytea.frogartha.exceptions.PlayerKickException;
+
+public class SerializerUtils {
+ public static void registerTypes(ManualSerializer serializer) {
+ serializer.register(new Acknowledge());
+ serializer.register(new Identity());
+ serializer.register(new PlayerKickException());
+ }
+}