fixed automatic connection reestablishing for web and tcp

This commit is contained in:
sergeych committed 2024-06-18 16:11:13 +07:00
1 parent 825c0bd5f7
commit 38fbca955c
6 files changed
+45 -41

No files matched your search

@@ -7,7 +7,6 @@ import io.ktor.websocket.*
import kotlinx.coroutines.cancel
import kotlinx.coroutines.channels.Channel
import kotlinx.coroutines.channels.ClosedReceiveChannelException
import kotlinx.coroutines.channels.ClosedSendChannelException
import kotlinx.coroutines.isActive
import kotlinx.coroutines.launch
import net.sergeych.crypto2.SigningKey
@@ -36,7 +35,6 @@ fun <S> Application.setupWebsocketServer(
val counter = AtomicCounter()
routing {
webSocket(path) {
println("--------------------------------------------")
val log = LogTag("KWS:${counter.incrementAndGet()}")
log.debug { "opening the connection" }
val input = Channel<UByteArray>(256)
@@ -44,7 +42,13 @@ fun <S> Application.setupWebsocketServer(
launch {
log.debug { "starting output pump" }
while (isActive) {
send(output.receive().toByteArray())
try {
send(output.receive().toByteArray())
}
catch(_: ClosedReceiveChannelException) {
log.debug { "closing output pump as output channel is closed" }
break
}
}
log.debug { "closing output pump" }
}
@@ -54,7 +58,10 @@ fun <S> Application.setupWebsocketServer(
createSession(),
serverKey
)
launch { server.run() }
launch {
server.run()
close()
}
log.debug { "KSC started, looking for incoming frames" }
for (f in incoming) {
log.debug { "incoming frame: ${f.frameType}" }
@@ -64,7 +71,7 @@ fun <S> Application.setupWebsocketServer(
log.debug { "in frame\n${it.toDump()}" }
})
} catch (_: RemoteInterface.ClosedException) {
log.info { "caught local closed exception, closing" }
log.warning { "caught local closed exception (strange!), closing" }
break
} catch (_: ClosedReceiveChannelException) {
log.info { "receive channel is closed, closing connection" }
@@ -77,9 +84,9 @@ fun <S> Application.setupWebsocketServer(
log.warning { "unknown frame type ${f.frameType}, ignoring" }
}
log.debug { "closing the server" }
println("****************prec")
close()
cancel()
println("****************postc")
log.debug { "server wbesock processing done" }
}
}
}