Update to ktor 2.

This commit is contained in:
Oliver Rümpelein 2022-05-26 11:26:31 +02:00
parent 51cb1dee9b
commit 59c8cae39b
15 changed files with 49 additions and 47 deletions

View file

@ -24,9 +24,9 @@ Now, they only need to enter their name on top, and they're set.
To trigger the buzzer, they just need to push the large surface. After a short amount of time they will be notified whether they were the fastest players (button turns green) or not (it turns red). To trigger the buzzer, they just need to push the large surface. After a short amount of time they will be notified whether they were the fastest players (button turns green) or not (it turns red).
### Game-Master-Client ### Game-Moderator-Client
You can access this client from `http://<yourhost>:8888/clients/master/`. You can access this client from `http://<yourhost>:8888/clients/moderator/`.
Your surface consists of three parts. After a game has started, and some player decided they want to do the turn, these have the following functions: Your surface consists of three parts. After a game has started, and some player decided they want to do the turn, these have the following functions:
* On top, there's a large surface showing you the fastest player if any. * On top, there's a large surface showing you the fastest player if any.

View file

@ -12,23 +12,25 @@ plugins {
} }
group = "de.pheerai.buzzer" group = "de.pheerai.buzzer"
version = "0.0.2" version = "0.0.3"
application { application {
mainClass.set("de.pheerai.buzzer.ApplicationKt") mainClass.set("de.pheerai.buzzer.ApplicationKt")
applicationDefaultJvmArgs = listOf("--add-exports=java.base/jdk.internal.misc=ALL-UNNAMED", "-Dio.netty.tryReflectionSetAccessible=true") applicationDefaultJvmArgs =
listOf("--add-exports=java.base/jdk.internal.misc=ALL-UNNAMED", "-Dio.netty.tryReflectionSetAccessible=true")
} }
repositories { repositories {
mavenCentral() mavenCentral()
google()
} }
dependencies { dependencies {
implementation("io.ktor:ktor-server-core:$ktor_version") implementation("io.ktor:ktor-server-core:$ktor_version")
implementation("io.ktor:ktor-websockets:$ktor_version")
implementation("org.jetbrains.kotlinx:kotlinx-html-jvm:$kotlinx_html_version")
implementation("io.ktor:ktor-html-builder:$ktor_version")
implementation("io.ktor:ktor-server-netty:$ktor_version") implementation("io.ktor:ktor-server-netty:$ktor_version")
implementation("io.ktor:ktor-server-websockets:$ktor_version")
implementation("io.ktor:ktor-server-html-builder:$ktor_version")
implementation("org.jetbrains.kotlinx:kotlinx-html-jvm:$kotlinx_html_version")
implementation("ch.qos.logback:logback-classic:$logback_version") implementation("ch.qos.logback:logback-classic:$logback_version")
testImplementation("io.ktor:ktor-server-tests:$ktor_version") testImplementation("io.ktor:ktor-server-tests:$ktor_version")
testImplementation("org.jetbrains.kotlin:kotlin-test:$kotlin_version") testImplementation("org.jetbrains.kotlin:kotlin-test:$kotlin_version")

View file

@ -1,5 +1,6 @@
ktor_version=1.6.4 ktor_version=2.0.1
kotlin_version=1.6.0 kotlin_version=1.6.20
logback_version=1.2.7 logback_version=1.2.11
kotlinx_html_version=0.7.3 kotlinx_html_version=0.7.5
shadowjar_version=7.1.2
kotlin.code.style=official kotlin.code.style=official

View file

@ -1,5 +1,5 @@
distributionBase=GRADLE_USER_HOME distributionBase=GRADLE_USER_HOME
distributionPath=wrapper/dists distributionPath=wrapper/dists
distributionUrl=https\://services.gradle.org/distributions/gradle-7.3-all.zip distributionUrl=https\://services.gradle.org/distributions/gradle-7.4.2-all.zip
zipStoreBase=GRADLE_USER_HOME zipStoreBase=GRADLE_USER_HOME
zipStorePath=wrapper/dists zipStorePath=wrapper/dists

View file

@ -2,7 +2,9 @@ rootProject.name = "server"
pluginManagement { pluginManagement {
plugins { plugins {
kotlin("jvm") version "1.6.0" val kotlin_version: String by settings
id("com.github.johnrengelman.shadow") version "7.1.0" val shadowjar_version: String by settings
kotlin("jvm") version kotlin_version
id("com.github.johnrengelman.shadow") version shadowjar_version
} }
} }

View file

@ -3,7 +3,7 @@ package de.pheerai.buzzer.clients
import de.pheerai.buzzer.alpine.* import de.pheerai.buzzer.alpine.*
import kotlinx.html.* import kotlinx.html.*
fun HTML.createGameMasterDocument() { fun HTML.createGameModeratorDocument() {
head { head {
meta { charset = "UTF-8" } meta { charset = "UTF-8" }
title { +"JS Client" } title { +"JS Client" }
@ -13,7 +13,7 @@ fun HTML.createGameMasterDocument() {
type = "text/css" type = "text/css"
) )
link( link(
href = "/assets/gamemaster.css", href = "/assets/gamemoderator.css",
rel = "stylesheet", rel = "stylesheet",
type = "text/css" type = "text/css"
) )
@ -30,7 +30,7 @@ fun HTML.createGameMasterDocument() {
//language=JavaScript //language=JavaScript
""" """
() => { () => {
startWebSocket('/socket/master', (ev) => { startWebSocket('/socket/moderator', (ev) => {
if (receivedNames.length === 0) { if (receivedNames.length === 0) {
firstPlayer = ev.data firstPlayer = ev.data
} }

View file

@ -1,8 +1,8 @@
package de.pheerai.buzzer.data package de.pheerai.buzzer.data
import io.ktor.http.cio.websocket.* import io.ktor.websocket.*
@JvmInline @JvmInline
value class PlayerSocket(val session: WebSocketSession) value class PlayerSocket(val session: WebSocketSession)
@JvmInline @JvmInline
value class GameMasterSocket(val session: WebSocketSession) value class GameModeratorSocket(val session: WebSocketSession)

View file

@ -4,7 +4,7 @@ import java.util.*
object SessionStorage { object SessionStorage {
val playerSessions = synchronisedCollectionOf<PlayerSocket>() val playerSessions = synchronisedCollectionOf<PlayerSocket>()
val gameMasterSessions = synchronisedCollectionOf<GameMasterSocket>() val gameModeratorSessions = synchronisedCollectionOf<GameModeratorSocket>()
} }
fun <T : Any> synchronisedCollectionOf(vararg data: T): MutableCollection<T> { fun <T : Any> synchronisedCollectionOf(vararg data: T): MutableCollection<T> {

View file

@ -1,13 +1,13 @@
package de.pheerai.buzzer.handlers package de.pheerai.buzzer.handlers
import de.pheerai.buzzer.data.GameMasterSocket import de.pheerai.buzzer.data.GameModeratorSocket
import de.pheerai.buzzer.data.SessionStorage import de.pheerai.buzzer.data.SessionStorage
import io.ktor.http.cio.websocket.* import io.ktor.websocket.*
import kotlinx.coroutines.isActive import kotlinx.coroutines.isActive
import org.slf4j.Logger import org.slf4j.Logger
suspend fun WebSocketSession.handleGameMasterSocket(log: Logger) { suspend fun WebSocketSession.handleGameModeratorSocket(log: Logger) {
SessionStorage.gameMasterSessions.add(GameMasterSocket(this)) SessionStorage.gameModeratorSessions.add(GameModeratorSocket(this))
for (frame in incoming) { for (frame in incoming) {
when (frame) { when (frame) {
is Frame.Text -> { is Frame.Text -> {

View file

@ -2,7 +2,7 @@ package de.pheerai.buzzer.handlers
import de.pheerai.buzzer.data.PlayerSocket import de.pheerai.buzzer.data.PlayerSocket
import de.pheerai.buzzer.data.SessionStorage import de.pheerai.buzzer.data.SessionStorage
import io.ktor.http.cio.websocket.* import io.ktor.websocket.*
import kotlinx.coroutines.isActive import kotlinx.coroutines.isActive
import org.slf4j.Logger import org.slf4j.Logger
@ -13,7 +13,7 @@ suspend fun WebSocketSession.handlePlayerSocket(log: Logger) {
is Frame.Text -> { is Frame.Text -> {
val text = frame.readText() val text = frame.readText()
log.info("Username: $text") log.info("Username: $text")
SessionStorage.gameMasterSessions.filter { it.session.isActive } SessionStorage.gameModeratorSessions.filter { it.session.isActive }
.forEach { it.session.send(text) } .forEach { it.session.send(text) }
} }
else -> {} else -> {}

View file

@ -1,27 +1,27 @@
package de.pheerai.buzzer.routing package de.pheerai.buzzer.routing
import de.pheerai.buzzer.clients.createGameMasterDocument import de.pheerai.buzzer.clients.createGameModeratorDocument
import de.pheerai.buzzer.clients.createPlayerDocument import de.pheerai.buzzer.clients.createPlayerDocument
import de.pheerai.buzzer.handlers.handleGameMasterSocket import de.pheerai.buzzer.handlers.handleGameModeratorSocket
import de.pheerai.buzzer.handlers.handlePlayerSocket import de.pheerai.buzzer.handlers.handlePlayerSocket
import io.ktor.application.* import io.ktor.server.application.*
import io.ktor.html.* import io.ktor.server.html.*
import io.ktor.http.content.* import io.ktor.server.http.content.*
import io.ktor.routing.* import io.ktor.server.routing.*
import io.ktor.websocket.* import io.ktor.server.websocket.*
import org.slf4j.Logger import org.slf4j.Logger
fun Application.configureRoutes() { fun Application.configureRoutes() {
routing { routing {
assetRoutes() assetRoutes()
clientRoutes() clientRoutes()
websocketRoutes(log) websocketRoutes(this@configureRoutes.log)
} }
} }
private fun Routing.websocketRoutes(log: Logger) { private fun Routing.websocketRoutes(log: Logger) {
webSocket("/socket/player") { handlePlayerSocket(log) } webSocket("/socket/player") { handlePlayerSocket(log) }
webSocket("/socket/master") { handleGameMasterSocket(log) } webSocket("/socket/moderator") { handleGameModeratorSocket(log) }
} }
private fun Routing.clientRoutes() { private fun Routing.clientRoutes() {
@ -37,9 +37,9 @@ private fun Routing.clientRoutes() {
} }
} }
get("/client/master") { get("/client/moderator") {
call.respondHtml { call.respondHtml {
createGameMasterDocument() createGameModeratorDocument()
} }
} }
} }

View file

@ -1,6 +1,6 @@
package de.pheerai.buzzer.setup package de.pheerai.buzzer.setup
import io.ktor.application.* import io.ktor.server.application.*
fun Application.setupPlugins() { fun Application.setupPlugins() {
installWebSockets() installWebSockets()

View file

@ -1,8 +1,7 @@
package de.pheerai.buzzer.setup package de.pheerai.buzzer.setup
import io.ktor.application.* import io.ktor.server.application.*
import io.ktor.http.cio.websocket.* import io.ktor.server.websocket.*
import io.ktor.websocket.*
import java.time.Duration import java.time.Duration
fun Application.installWebSockets() { fun Application.installWebSockets() {

View file

@ -1,12 +1,10 @@
module buzzer { module buzzer {
requires ktor.server.host.common.jvm;
requires ktor.server.netty.jvm;
requires ktor.server.core.jvm;
requires ktor.websockets.jvm;
requires ktor.html.builder.jvm;
requires org.slf4j; requires org.slf4j;
requires ktor.http.cio.jvm;
requires kotlinx.coroutines.core.jvm; requires kotlinx.coroutines.core.jvm;
requires kotlin.stdlib; requires kotlin.stdlib;
requires kotlinx.html.jvm; requires kotlinx.html.jvm;
requires io.ktor.websockets;
requires io.ktor.server.core;
requires io.ktor.server.websockets;
requires io.ktor.server.html.builder;
} }