change the isOnlineCheck to prepare for nostr nests websocket-based streaming.

This commit is contained in:
Vitor Pamplona
2024-03-13 17:57:31 -04:00
parent f2dc2ef0d0
commit 45974fb09b
@@ -24,7 +24,11 @@ import android.util.Log
import android.util.LruCache import android.util.LruCache
import androidx.compose.runtime.Immutable import androidx.compose.runtime.Immutable
import com.vitorpamplona.amethyst.BuildConfig import com.vitorpamplona.amethyst.BuildConfig
import com.vitorpamplona.quartz.crypto.CryptoUtils
import okhttp3.EventListener
import okhttp3.Protocol
import okhttp3.Request import okhttp3.Request
import okio.ByteString.Companion.toByteString
import kotlin.coroutines.cancellation.CancellationException import kotlin.coroutines.cancellation.CancellationException
@Immutable data class OnlineCheckResult(val timeInMs: Long, val online: Boolean) @Immutable data class OnlineCheckResult(val timeInMs: Long, val online: Boolean)
@@ -49,9 +53,31 @@ object OnlineChecker {
return checkOnlineCache.get(url).online return checkOnlineCache.get(url).online
} }
Log.d("OnlineChecker", "isOnline $url")
return try { return try {
val result =
if (url.startsWith("wss")) {
val request =
Request.Builder()
.header("User-Agent", "Amethyst/${BuildConfig.VERSION_NAME}")
.url(url.replace("wss+livekit://", "wss://"))
.header("Upgrade", "websocket")
.header("Connection", "Upgrade")
.header("Sec-WebSocket-Key", CryptoUtils.random(16).toByteString().base64())
.header("Sec-WebSocket-Version", "13")
.header("Sec-WebSocket-Extensions", "permessage-deflate")
.build()
val client =
HttpClientManager.getHttpClient().newBuilder()
.eventListener(EventListener.NONE)
.protocols(listOf(Protocol.HTTP_1_1))
.build()
client.newCall(request).execute().use {
checkNotInMainThread()
it.isSuccessful
}
} else {
val request = val request =
Request.Builder() Request.Builder()
.header("User-Agent", "Amethyst/${BuildConfig.VERSION_NAME}") .header("User-Agent", "Amethyst/${BuildConfig.VERSION_NAME}")
@@ -59,11 +85,12 @@ object OnlineChecker {
.get() .get()
.build() .build()
val result =
HttpClientManager.getHttpClient().newCall(request).execute().use { HttpClientManager.getHttpClient().newCall(request).execute().use {
checkNotInMainThread() checkNotInMainThread()
it.isSuccessful it.isSuccessful
} }
}
checkOnlineCache.put(url, OnlineCheckResult(System.currentTimeMillis(), result)) checkOnlineCache.put(url, OnlineCheckResult(System.currentTimeMillis(), result))
result result
} catch (e: Exception) { } catch (e: Exception) {