From f3d2f235801d12ae500ea5ece8fbc18ee1639e17 Mon Sep 17 00:00:00 2001 From: Yiqun Zhang Date: Fri, 1 Dec 2023 14:00:27 +0800 Subject: [PATCH] :bug: Fix clipserver get port 0 error (#117) --- .../com/clipevery/net/DesktopClipServer.kt | 16 ++++++++-------- 1 file changed, 8 insertions(+), 8 deletions(-) diff --git a/composeApp/src/desktopMain/kotlin/com/clipevery/net/DesktopClipServer.kt b/composeApp/src/desktopMain/kotlin/com/clipevery/net/DesktopClipServer.kt index b9640e508..e1583b665 100644 --- a/composeApp/src/desktopMain/kotlin/com/clipevery/net/DesktopClipServer.kt +++ b/composeApp/src/desktopMain/kotlin/com/clipevery/net/DesktopClipServer.kt @@ -5,7 +5,6 @@ import com.clipevery.model.AppHostInfo import com.clipevery.model.AppRequestBindInfo import com.clipevery.platform.currentPlatform import io.github.oshai.kotlinlogging.KotlinLogging -import io.ktor.server.application.ApplicationStarted import io.ktor.server.application.call import io.ktor.server.engine.embeddedServer import io.ktor.server.netty.Netty @@ -22,6 +21,8 @@ class DesktopClipServer(private val signalProtocol: SignalProtocol): ClipServer private val logger = KotlinLogging.logger {} + private var port = 0 + private var server: NettyApplicationEngine = embeddedServer(Netty, port = 0) { routing { @@ -33,11 +34,11 @@ class DesktopClipServer(private val signalProtocol: SignalProtocol): ClipServer override fun start(): ClipServer { server.start(wait = false) - runBlocking { - server.environment.monitor.subscribe(ApplicationStarted) { - val actualPort = server.environment.connectors.map { it.port }.first() - logger.info { "Server started on port: $actualPort" } - } + port = runBlocking { server.resolvedConnectors().first().port } + if (port == 0) { + logger.error { "Failed to start server" } + } else { + logger.info { "Server started at port $port" } } return this } @@ -47,10 +48,9 @@ class DesktopClipServer(private val signalProtocol: SignalProtocol): ClipServer } override fun port(): Int { - return server.environment.connectors.first().port + return port } - private fun getHostInfoList(): List { val nets = NetworkInterface.getNetworkInterfaces()