feat(ui): Websocket
Added simple websocket. Serverside websocket logic isnt in the usersession
This commit is contained in:
@@ -0,0 +1,21 @@
|
|||||||
|
package actor
|
||||||
|
|
||||||
|
import org.apache.pekko.actor.{Actor, ActorRef}
|
||||||
|
import org.apache.pekko.http.scaladsl.model.ContentRange.Other
|
||||||
|
|
||||||
|
|
||||||
|
class KnockOutWebSocketActor(
|
||||||
|
out: ActorRef,
|
||||||
|
) extends Actor {
|
||||||
|
def receive: Receive = {
|
||||||
|
case msg: String =>
|
||||||
|
out ! s"Received your message: ${msg}"
|
||||||
|
case other: Other =>
|
||||||
|
println(s"Received unknown message: $other")
|
||||||
|
}
|
||||||
|
|
||||||
|
def sendJsonToClient(json: String): Unit = {
|
||||||
|
println("Received event from Controller")
|
||||||
|
out ! json
|
||||||
|
}
|
||||||
|
}
|
||||||
30
knockoutwhistweb/app/controllers/WebsocketController.scala
Normal file
30
knockoutwhistweb/app/controllers/WebsocketController.scala
Normal file
@@ -0,0 +1,30 @@
|
|||||||
|
package controllers
|
||||||
|
import actor.KnockOutWebSocketActor
|
||||||
|
import org.apache.pekko.actor.{ActorRef, ActorSystem, Props}
|
||||||
|
import org.apache.pekko.stream.Materializer
|
||||||
|
import play.api.*
|
||||||
|
import play.api.libs.streams.ActorFlow
|
||||||
|
import play.api.mvc.*
|
||||||
|
|
||||||
|
import javax.inject.*
|
||||||
|
|
||||||
|
|
||||||
|
@Singleton
|
||||||
|
class WebsocketController @Inject()(
|
||||||
|
cc: ControllerComponents,
|
||||||
|
)(implicit system: ActorSystem, mat: Materializer) extends AbstractController(cc) {
|
||||||
|
|
||||||
|
object KnockOutWebSocketActorFactory {
|
||||||
|
def create(out: ActorRef) = {
|
||||||
|
Props(new KnockOutWebSocketActor(out))
|
||||||
|
}
|
||||||
|
}
|
||||||
|
def socket() = WebSocket.accept[String, String] { request =>
|
||||||
|
ActorFlow.actorRef { out =>
|
||||||
|
println("Connect received")
|
||||||
|
KnockOutWebSocketActorFactory.create(out)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
}
|
||||||
@@ -38,4 +38,7 @@ POST /game/:id/dogPlayCard controllers.IngameController.playDog
|
|||||||
POST /game/:id/returnToLobby controllers.IngameController.returnToLobby(id: String)
|
POST /game/:id/returnToLobby controllers.IngameController.returnToLobby(id: String)
|
||||||
|
|
||||||
# Polling
|
# Polling
|
||||||
GET /polling/:gameId controllers.PollingController.polling(gameId: String)
|
GET /polling/:gameId controllers.PollingController.polling(gameId: String)
|
||||||
|
|
||||||
|
# Websocket
|
||||||
|
GET /websocket controllers.WebsocketController.socket()
|
||||||
@@ -664,4 +664,23 @@ function sendPlayCardRequest(jsonObj, gameId, cardobject, dog) {
|
|||||||
})
|
})
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
const ws = new WebSocket("ws://localhost:9000/websocket");
|
||||||
|
ws.onopen = (event) => {
|
||||||
|
console.log("WebSocket connection established!");
|
||||||
|
|
||||||
|
ws.send("Client is now connected and ready.");
|
||||||
|
};
|
||||||
|
ws.onmessage = (event) => {
|
||||||
|
console.log("SERVER RESPONSE:", event.data);
|
||||||
|
};
|
||||||
|
ws.onerror = (error) => {
|
||||||
|
console.error("WebSocket Error:", error);
|
||||||
|
};
|
||||||
|
ws.onclose = (event) => {
|
||||||
|
if (event.wasClean) {
|
||||||
|
console.log(`Connection closed cleanly, code=${event.code} reason=${event.reason}`);
|
||||||
|
} else {
|
||||||
|
console.warn('Connection died unexpectedly.');
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user