+client.reconnect()

This commit is contained in:
sergeych committed 2022-09-08 12:03:40 +03:00
1 parent a671414b55
commit 39aa031bf0
2 files changed
+11

No files matched your search

@@ -11,5 +11,7 @@ interface Parsec3Transport<S> {
val connectedFlow: StateFlow<Boolean> val connectedFlow: StateFlow<Boolean>
fun close() fun close()
fun reconnect()
suspend fun adapter(): Adapter<S> suspend fun adapter(): Adapter<S>
} }
@@ -22,6 +22,7 @@ class Parsec3WSClient<S, H : CommandHost<S>>(
private val _connectionFlow = MutableStateFlow(false) private val _connectionFlow = MutableStateFlow(false)
private val closeFlow = MutableStateFlow(false) private val closeFlow = MutableStateFlow(false)
private val reconnectFlow = MutableStateFlow(false)
override val connectedFlow: StateFlow<Boolean> = _connectionFlow override val connectedFlow: StateFlow<Boolean> = _connectionFlow
@@ -33,6 +34,10 @@ class Parsec3WSClient<S, H : CommandHost<S>>(
if( closeFlow.value == false ) closeFlow.value = true if( closeFlow.value == false ) closeFlow.value = true
} }
override fun reconnect() {
reconnectFlow.value = true
}
var deferredAdapter = CompletableDeferred<Adapter<S>>() var deferredAdapter = CompletableDeferred<Adapter<S>>()
private set private set
@@ -40,11 +45,14 @@ class Parsec3WSClient<S, H : CommandHost<S>>(
fun start() { fun start() {
globalLaunch { globalLaunch {
while(closeFlow.value != true) {
reconnectFlow.value = false
client.webSocket(url) { client.webSocket(url) {
info { "Connected to $url" } info { "Connected to $url" }
val a = builder.create { send(Frame.Binary(true, it)) } val a = builder.create { send(Frame.Binary(true, it)) }
_connectionFlow.value = true _connectionFlow.value = true
launch { closeFlow.collect { if (it == true) close() } } launch { closeFlow.collect { if (it == true) close() } }
launch { reconnectFlow.collect { if (it == true) close() } }
deferredAdapter.complete(a) deferredAdapter.complete(a)
for (f in incoming) for (f in incoming)
if (f is Frame.Binary) a.receiveFrame(f.data) if (f is Frame.Binary) a.receiveFrame(f.data)
@@ -55,6 +63,7 @@ class Parsec3WSClient<S, H : CommandHost<S>>(
} }
} }
} }
}
companion object { companion object {
private val client = HttpClient { private val client = HttpClient {