fix(android): switch Ktor CIO engine to OkHttp for stable background WebSocket
CIO uses Java NIO selectors which Android's power management freezes when the app is backgrounded, dropping the WebSocket connection. OkHttp is Android-native and maintains connections through the OS network stack.
This commit is contained in:
@@ -48,7 +48,7 @@ dependencies {
|
||||
implementation(libs.androidx.compose.material3)
|
||||
|
||||
// Ktor WebSocket
|
||||
implementation(libs.ktor.client.cio)
|
||||
implementation(libs.ktor.client.okhttp)
|
||||
implementation(libs.ktor.client.websockets)
|
||||
implementation(libs.ktor.client.content.negotiation)
|
||||
implementation(libs.ktor.serialization.kotlinx.json)
|
||||
|
||||
@@ -6,8 +6,7 @@ import com.thisux.droidclaw.model.ConnectionState
|
||||
import com.thisux.droidclaw.model.DeviceInfoMsg
|
||||
import com.thisux.droidclaw.model.ServerMessage
|
||||
import io.ktor.client.HttpClient
|
||||
import io.ktor.client.engine.cio.CIO
|
||||
import io.ktor.client.engine.cio.endpoint
|
||||
import io.ktor.client.engine.okhttp.OkHttp
|
||||
import io.ktor.client.plugins.websocket.WebSockets
|
||||
import io.ktor.client.plugins.websocket.webSocket
|
||||
import io.ktor.websocket.Frame
|
||||
@@ -78,14 +77,7 @@ class ReliableWebSocket(
|
||||
}
|
||||
|
||||
private suspend fun connectOnce(serverUrl: String, apiKey: String, deviceInfo: DeviceInfoMsg) {
|
||||
val httpClient = HttpClient(CIO) {
|
||||
engine {
|
||||
requestTimeout = 30_000
|
||||
endpoint {
|
||||
connectTimeout = 10_000
|
||||
keepAliveTime = 30_000
|
||||
}
|
||||
}
|
||||
val httpClient = HttpClient(OkHttp) {
|
||||
install(WebSockets) {
|
||||
pingIntervalMillis = 30_000
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user