+client.reconnect()
This commit is contained in:
parent
a671414b55
commit
39aa031bf0
@ -11,5 +11,7 @@ interface Parsec3Transport<S> {
|
||||
val connectedFlow: StateFlow<Boolean>
|
||||
fun close()
|
||||
|
||||
fun reconnect()
|
||||
|
||||
suspend fun adapter(): Adapter<S>
|
||||
}
|
@ -22,6 +22,7 @@ class Parsec3WSClient<S, H : CommandHost<S>>(
|
||||
|
||||
private val _connectionFlow = MutableStateFlow(false)
|
||||
private val closeFlow = MutableStateFlow(false)
|
||||
private val reconnectFlow = MutableStateFlow(false)
|
||||
override val connectedFlow: StateFlow<Boolean> = _connectionFlow
|
||||
|
||||
|
||||
@ -33,6 +34,10 @@ class Parsec3WSClient<S, H : CommandHost<S>>(
|
||||
if( closeFlow.value == false ) closeFlow.value = true
|
||||
}
|
||||
|
||||
override fun reconnect() {
|
||||
reconnectFlow.value = true
|
||||
}
|
||||
|
||||
var deferredAdapter = CompletableDeferred<Adapter<S>>()
|
||||
private set
|
||||
|
||||
@ -40,11 +45,14 @@ class Parsec3WSClient<S, H : CommandHost<S>>(
|
||||
|
||||
fun start() {
|
||||
globalLaunch {
|
||||
while(closeFlow.value != true) {
|
||||
reconnectFlow.value = false
|
||||
client.webSocket(url) {
|
||||
info { "Connected to $url" }
|
||||
val a = builder.create { send(Frame.Binary(true, it)) }
|
||||
_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)
|
||||
for (f in incoming)
|
||||
if (f is Frame.Binary) a.receiveFrame(f.data)
|
||||
@ -55,6 +63,7 @@ class Parsec3WSClient<S, H : CommandHost<S>>(
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
companion object {
|
||||
private val client = HttpClient {
|
||||
|
Loading…
Reference in New Issue
Block a user