melatonina
02/01/2021, 10:10 AMe5l
02/01/2021, 10:11 AMmelatonina
02/01/2021, 10:12 AMBig Chungus
02/01/2021, 10:20 AMmelatonina
02/01/2021, 10:21 AMio.ktor.server.netty.EngineMain.main(emptyArray())
which are classified as "development engines" in the ktor documentation.e5l
02/01/2021, 10:22 AMmelatonina
02/01/2021, 10:22 AMe5l
02/01/2021, 10:28 AMmelatonina
02/01/2021, 10:30 AM<logger name="io.netty" level="INFO"/>
Ktor version is 1.5.1:
ktor_version=1.5.1
e5l
02/01/2021, 10:31 AMmelatonina
02/01/2021, 10:32 AMe5l
02/01/2021, 10:32 AMmelatonina
02/01/2021, 3:07 PMimport java.util.*
import kotlin.collections.LinkedHashSet
import io.ktor.application.*
import io.ktor.http.cio.websocket.*
import io.ktor.routing.*
import io.ktor.websocket.*
import io.ktor.application.Application
fun Application.module() {
install(WebSockets)
routing {
webSocket("/ktor_fx") {
val connection = Connection(this)
KtorFxService.connections += connection
try {
connection.send("You are connected!")
for(frame in incoming) {
if (frame is Frame.Text) {
val receivedText = frame.readText()
connection.send("You said: \"$receivedText\"")
connection.send("${KtorFxService::class.simpleName}.connectionCount = ${KtorFxService.connectionCount}")
}
}
} finally {
KtorFxService.connections -= connection
}
}
}
}
class Connection(
val session: DefaultWebSocketServerSession
) {
suspend fun send(text: String) {
session.send(text)
}
}
object KtorFxService {
init {
println("Creating a new ${KtorFxService::class.simpleName}")
}
val connections = Collections.synchronizedSet<Connection>(LinkedHashSet())
val connectionCount get() = connections.size
suspend fun broadcast(text: String) {
println("Broadcasting \"$text\" to ${connections.size} clients")
// We should synchronize(connection) but we can't because send is a suspension point
// and can't be inside a synchronize block.
// This is another issue
connections.forEach { it.send(text) }
}
}
import kotlinx.coroutines.*
import kotlinx.coroutines.javafx.JavaFx
import javafx.application.Application
import javafx.event.EventHandler
import javafx.scene.Scene
import javafx.scene.control.Button
import javafx.scene.layout.VBox
import javafx.stage.Stage
import kotlin.random.Random
class KtorFxApplication : Application() {
val coroutineScope = CoroutineScope(Dispatchers.JavaFx)
override fun start(primaryStage: Stage) {
primaryStage.apply {
title = "${KtorFxApplication::class.simpleName}"
scene = Scene(
VBox().apply {
children.setAll(
Button().apply {
text = "Start service"
onAction = EventHandler {
coroutineScope.launch(<http://Dispatchers.IO|Dispatchers.IO>) {
io.ktor.server.netty.EngineMain.main(emptyArray())
}
}
},
Button().apply {
text = "Broadcast something"
onAction = EventHandler {
coroutineScope.launch(<http://Dispatchers.IO|Dispatchers.IO>) {
KtorFxService.broadcast("${Random.nextInt()}")
}
}
}
)
},
300.0, 250.0
)
show()
}
}
companion object {
@JvmStatic
fun main(args: Array<String>) {
launch(KtorFxApplication::class.java, *args)
}
}
}
CONNECTED
RECEIVED: You are connected!
SENT: Hi!
RECEIVED: You said: "Hi!"
RECEIVED: KtorFxService.connectionCount = 1
When I try to broadcast something, the console says:
Creating a new KtorFxService
Creating a new KtorFxService
Broadcasting "-1198330446" to 0 clients
The first creation of KtorFxService happens when I connect a client to the service. The second happens when I attempt to broadcast.