Step 1: Send "first" Player or clear to server.

This commit is contained in:
Oliver Rümpelein 2021-09-26 16:11:03 +02:00
parent 847d09e734
commit 67b5bf5fe0
4 changed files with 36 additions and 4 deletions

View file

@ -43,3 +43,7 @@ fun HTMLTag.xText(text: String) {
fun HTMLTag.xBind(type: String, data: String) {
attributes["x-bind:$type"] = data
}
fun HTMLTag.xEffect(effect: String) {
attributes["x-effect"] = effect
}

View file

@ -19,20 +19,46 @@ fun HTML.createGameMasterDocument() {
script { src = "/assets/buzzerWebSocket.js" }
}
body {
xData("{receivedNames: [], buzzerWebSocket: null}")
xData("{receivedNames: [], buzzerWebSocket: null, firstPlayer: null }")
xInit(
//language=JavaScript
"""
() => {
startWebSocket('/socket/master', (ev) => {
if (receivedNames.length === 0) {
firstPlayer = ev.data
}
receivedNames.push(ev.data)
});
}
""".trimIndent()
)
xEffect(
//language=JavaScript
"""
if (Alpine.data.buzzerWebSocket.readyState === 1) {
if (firstPlayer !== null) {
console.log("Player")
Alpine.data.buzzerWebSocket.send(`PLAYER:${"$"}{firstPlayer}`);
} else {
console.log("Clear")
Alpine.data.buzzerWebSocket.send("CLEAR:");
}
}
""".trimIndent()
)
div(classes = "parent") {
button {
xOn("click", "receivedNames = []")
xOn(
"click",
//language=JavaScript
"""
() => {
receivedNames = []
firstPlayer = null
}
""".trimIndent()
)
+"Reset"
}
ol {

View file

@ -4,13 +4,15 @@ import de.pheerai.buzzer.data.GameMasterSocket
import de.pheerai.buzzer.data.SessionStorage
import io.ktor.http.cio.websocket.*
import kotlinx.coroutines.isActive
import org.slf4j.Logger
suspend fun WebSocketSession.handleGameMasterSocket() {
suspend fun WebSocketSession.handleGameMasterSocket(log: Logger) {
SessionStorage.gameMasterSessions.add(GameMasterSocket(this))
for (frame in incoming) {
when (frame) {
is Frame.Text -> {
val text = frame.readText()
log.info("GM Socket received $text")
SessionStorage.playerSessions.filter { it.session.isActive }
.forEach { it.session.send(text) }
}

View file

@ -21,7 +21,7 @@ fun Application.configureRoutes() {
private fun Routing.websocketRoutes(log: Logger) {
webSocket("/socket/player") { handlePlayerSocket(log) }
webSocket("/socket/master") { handleGameMasterSocket() }
webSocket("/socket/master") { handleGameMasterSocket(log) }
}
private fun Routing.clientRoutes() {