feat(base): Fixed logic for websockets and added GameStateEvent. Might've caused instability on other feature branches! (#84)

Reviewed-on: #84
Reviewed-by: lq64 <lq@blackhole.local>
Co-authored-by: Janis <janis.e.20@gmx.de>
Co-committed-by: Janis <janis.e.20@gmx.de>
This commit is contained in:
2025-11-26 11:26:08 +01:00
committed by lq64
parent 52e5033afc
commit b81bb3d0ae
14 changed files with 200 additions and 134 deletions

View File

@@ -12,13 +12,19 @@ class UserWebsocketActor(
session: UserSession
) extends Actor {
if (session.websocketActor.isDefined) {
session.websocketActor.foreach(actor => actor.transmitTextToClient("Error: Multiple websocket connections detected. Closing this connection."))
context.stop(self)
} else {
{
session.lock.lock()
if (session.websocketActor.isDefined) {
val otherWebsocket = session.websocketActor.get
otherWebsocket.transmitTextToClient("Error: Multiple websocket connections detected. Closing your connection.")
context.stop(otherWebsocket.self)
transmitTextToClient("Previous websocket connection closed. You are now connected.")
}
session.websocketActor = Some(this)
session.lock.unlock()
}
override def receive: Receive = {
case msg: String =>
val jsonObject = Try {
@@ -86,12 +92,12 @@ class UserWebsocketActor(
}
}
def transmitJsonToClient(jsonObj: JsObject): Unit = {
def transmitJsonToClient(jsonObj: JsValue): Unit = {
transmitTextToClient(jsonObj.toString())
}
def transmitEventToClient(event: SimpleEvent): Unit = {
transmitJsonToClient(WebsocketEventMapper.toJson(event))
transmitJsonToClient(WebsocketEventMapper.toJson(event, session))
}
}