Skip to content

Commit

Permalink
🐛 Fix clipserver get port 0 error (#117)
Browse files Browse the repository at this point in the history
  • Loading branch information
guiyanakuang authored Dec 1, 2023
1 parent 77d38c9 commit f3d2f23
Showing 1 changed file with 8 additions and 8 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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 {
Expand All @@ -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
}
Expand All @@ -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<AppHostInfo> {
val nets = NetworkInterface.getNetworkInterfaces()

Expand Down

0 comments on commit f3d2f23

Please sign in to comment.