Hello, This is my KMM Ktor client engine setup: ``...
# ktor
g
Hello, This is my KMM Ktor client engine setup:
Copy code
actual fun getHttpEngine(): HttpClientEngine = OkHttp.create {
        OkHttpConfig().also {
            addInterceptor(chuckerInterceptor)
            it.config {
                readTimeout(60, TimeUnit.SECONDS)
                connectTimeout(60, TimeUnit.SECONDS)
            }
        }
    }
but for some reason I’m receiving
failed with exception: io.ktor.network.sockets.SocketTimeoutException: Socket timeout has expired [url=..., socket_timeout=unknown] ms
after 10 seconds. Should I add this timeout settings in HttpClient? Thanks.
r
OkHttp.create { }
supplies its own config object as a
this
reference in the builder lambda. Try this instead
Copy code
actual fun getHttpEngine(): HttpClientEngine = OkHttp.create {
        addInterceptor(chuckerInterceptor)
        config {
            readTimeout(60, TimeUnit.SECONDS)
            connectTimeout(60, TimeUnit.SECONDS)
        }
    }
g
still getting
SocketTimeoutException
after 10s 😞
@russhwolf any other tip or check?
Copy code
private fun createHttpClient(
    engine: HttpClientEngine,
    networkLogger: co.touchlab.kermit.Logger? = null,
    tokenProvider: TokenProvider,
    customModule: SerializersModule? = null
): HttpClient {
    return HttpClient(engine) {
        if (networkLogger != null) {
            install(Logging) {
                logger = object : Logger {
                    override fun log(message: String) {
                        networkLogger.v("HTTP Client", message)
                    }
                }
                level = LogLevel.ALL
            }
        }
        install(JsonFeature) {
            serializer = KotlinxSerializer(kotlinx.serialization.json.Json {
                isLenient = true
                ignoreUnknownKeys = true
                if (customModule != null) {
                    serializersModule = customModule
                }
            })
        }
        defaultRequest {
            header(HttpHeaders.Authorization, "Bearer ${tokenProvider.getAccessToken()}")
            header(HttpHeaders.Connection, "Close")
        }
    }
}
my full client setup ^
r
not sure off-hand but there might be a timeout in the ktor configuration (rather than the okhttp configuration) that you need to set.
g
Copy code
install(HttpTimeout) {
    requestTimeoutMillis = 60000
    connectTimeoutMillis = 60000
    socketTimeoutMillis = 60000
}
well, this solved it ^^
129 Views