Manav Tamboli
03/01/2024, 12:55 PMfun Routing.getFlow() = callbackFlow<String> {
val route = this@getFlow
route.get("/example") {
try {
send("Received GET request at /example")
} catch (e: Exception) {
close(e)
}
}
awaitClose()
}
Is this the correct way (creating route inside a callback flow) to do it or does this have any drawbacks?Aleksei Tirman [JB]
03/01/2024, 3:03 PMManav Tamboli
03/02/2024, 6:46 AMManav Tamboli
03/02/2024, 6:47 AMAndreas Scheja
03/02/2024, 9:38 AMMutableSharedFlow
instead
embeddedServer(Netty, port = 8080) {
data class ReceivedRequest(
val httpMethod: HttpMethod,
val path: String,
)
val receivedRequests = MutableSharedFlow<ReceivedRequest>()
launch {
receivedRequests.collect { receivedRequest ->
println("Received request: $receivedRequest")
// save to database or whatever here
}
}
install(WebSockets)
routing {
webSocket("/ws") {
receivedRequests.collect { receivedRequest ->
send("received request $receivedRequest")
}
}
get("/example") {
receivedRequests.emit(ReceivedRequest(call.request.httpMethod, call.request.path()))
call.respondText("Hello World!")
}
}
}.start(true)