fix websocket connection

This commit is contained in:
Adam 2022-07-02 08:54:02 +02:00
parent c0c82e7c9c
commit 8c9d8e40c2

View file

@ -1,12 +1,13 @@
package dev.adamko.kafkatorio.server.web.websocket package dev.adamko.kafkatorio.server.web.websocket
import io.ktor.websocket.CloseReason
import io.ktor.websocket.DefaultWebSocketSession import io.ktor.websocket.DefaultWebSocketSession
import io.ktor.websocket.Frame import io.ktor.websocket.Frame
import io.ktor.websocket.send import io.ktor.websocket.send
import java.util.concurrent.atomic.AtomicInteger import java.util.concurrent.atomic.AtomicInteger
import kotlin.time.Duration.Companion.seconds import kotlin.time.Duration.Companion.seconds
import kotlin.time.TimeSource import kotlin.time.TimeSource
import kotlinx.coroutines.awaitCancellation import kotlinx.coroutines.cancel
import kotlinx.coroutines.channels.BufferOverflow import kotlinx.coroutines.channels.BufferOverflow
import kotlinx.coroutines.coroutineScope import kotlinx.coroutines.coroutineScope
import kotlinx.coroutines.delay import kotlinx.coroutines.delay
@ -63,7 +64,7 @@ class WebmapWebsocketServer {
client.incoming.receiveAsFlow().collect(messagesFromClients) client.incoming.receiveAsFlow().collect(messagesFromClients)
} }
// client.launch { client.launch {
while (isActive) { while (isActive) {
runCatching { runCatching {
client.send("elapsed ${startTime.elapsedNow()}") client.send("elapsed ${startTime.elapsedNow()}")
@ -71,9 +72,14 @@ class WebmapWebsocketServer {
delay(10.seconds) delay(10.seconds)
yield() yield()
} }
// } }
// awaitCancellation() log("client ${client.name} is up and running...")
val closedReason: CloseReason? = client.closeReason.await()
log("client ${client.name} closed ${closedReason ?: "successfully"}")
coroutineContext.cancel()
} }