+client.reconnect()
This commit is contained in:
parent
a671414b55
commit
39aa031bf0
@ -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,18 +45,22 @@ class Parsec3WSClient<S, H : CommandHost<S>>(
|
|||||||
|
|
||||||
fun start() {
|
fun start() {
|
||||||
globalLaunch {
|
globalLaunch {
|
||||||
client.webSocket(url) {
|
while(closeFlow.value != true) {
|
||||||
info { "Connected to $url" }
|
reconnectFlow.value = false
|
||||||
val a = builder.create { send(Frame.Binary(true, it)) }
|
client.webSocket(url) {
|
||||||
_connectionFlow.value = true
|
info { "Connected to $url" }
|
||||||
launch { closeFlow.collect { if( it == true ) close() } }
|
val a = builder.create { send(Frame.Binary(true, it)) }
|
||||||
deferredAdapter.complete(a)
|
_connectionFlow.value = true
|
||||||
for (f in incoming)
|
launch { closeFlow.collect { if (it == true) close() } }
|
||||||
if (f is Frame.Binary) a.receiveFrame(f.data)
|
launch { reconnectFlow.collect { if (it == true) close() } }
|
||||||
// when we leave connection will be closed. So far we do not close the adapter,
|
deferredAdapter.complete(a)
|
||||||
// should we?
|
for (f in incoming)
|
||||||
_connectionFlow.value = false
|
if (f is Frame.Binary) a.receiveFrame(f.data)
|
||||||
deferredAdapter = CompletableDeferred()
|
// when we leave connection will be closed. So far we do not close the adapter,
|
||||||
|
// should we?
|
||||||
|
_connectionFlow.value = false
|
||||||
|
deferredAdapter = CompletableDeferred()
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user