Waits 10 seconds before reconnecting.

This commit is contained in:
Vitor Pamplona 2023-01-27 20:06:34 -03:00
parent 1e3654396b
commit 5f3f80bc97
3 changed files with 14 additions and 5 deletions

View File

@ -15,6 +15,7 @@ import com.vitorpamplona.amethyst.service.model.RepostEvent
import com.vitorpamplona.amethyst.service.relays.Client
import com.vitorpamplona.amethyst.service.relays.Relay
import java.util.Collections
import java.util.UUID
import kotlin.time.ExperimentalTime
import kotlin.time.measureTimedValue
import kotlinx.coroutines.CoroutineScope
@ -140,7 +141,7 @@ abstract class NostrDataSource<T>(val debugName: String) {
}
fun requestNewChannel(): Channel {
val newChannel = Channel()
val newChannel = Channel(debugName+UUID.randomUUID().toString().substring(0,4))
channels.add(newChannel)
channelIds.add(newChannel.id)
return newChannel

View File

@ -3,6 +3,7 @@ package com.vitorpamplona.amethyst.service.relays
import android.util.Log
import com.google.gson.JsonElement
import com.vitorpamplona.amethyst.model.LocalCache
import java.util.Date
import nostr.postr.events.ContactListEvent
import nostr.postr.events.Event
import nostr.postr.toNpub
@ -25,6 +26,8 @@ class Relay(
var eventUploadCounter = 0
var errorCounter = 0
var closingTime = 0L
fun register(listener: Listener) {
listeners = listeners.plus(listener)
}
@ -92,6 +95,7 @@ class Relay(
override fun onClosed(webSocket: WebSocket, code: Int, reason: String) {
socket = null
closingTime = Date().time / 1000
listeners.forEach { it.onRelayStateChange(this@Relay, Type.DISCONNECT) }
}
@ -101,6 +105,8 @@ class Relay(
socket?.close(1000, "Normal close")
// Failures disconnect the relay.
socket = null
closingTime = Date().time / 1000
Log.w("Relay", "Relay onFailure ${url}, ${response?.message}")
//t.printStackTrace()
listeners.forEach {
@ -114,6 +120,7 @@ class Relay(
fun disconnect() {
//httpClient.dispatcher.executorService.shutdown()
closingTime = Date().time / 1000
socket?.close(1000, "Normal close")
socket = null
}
@ -121,13 +128,16 @@ class Relay(
fun sendFilter(requestId: String) {
if (read) {
if (socket == null) {
requestAndWatch()
// waits 10 seconds
if (Date().time / 1000 > closingTime + 10) {
requestAndWatch()
}
} else {
val filters = Client.getSubscriptionFilters(requestId)
if (filters.isNotEmpty()) {
val request =
"""["REQ","$requestId",${filters.joinToString(",") { it.toJson() }}]"""
//println("FILTERSSENT " + """["REQ","$requestId",${filters.joinToString(",") { it.toJson() }}]""")
//println("FILTERSSENT ${url} " + """["REQ","$requestId",${filters.joinToString(",") { it.toJson() }}]""")
socket?.send(request)
}
}

View File

@ -48,8 +48,6 @@ fun FeedView(viewModel: FeedViewModel, accountViewModel: AccountViewModel, navCo
}
}
println("FeedView Refresh ${feedState}")
SwipeRefresh(
state = swipeRefreshState,
onRefresh = {