+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,18 +45,22 @@ class Parsec3WSClient<S, H : CommandHost<S>>(
 | 
			
		||||
 | 
			
		||||
    fun start() {
 | 
			
		||||
        globalLaunch {
 | 
			
		||||
            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() } }
 | 
			
		||||
                deferredAdapter.complete(a)
 | 
			
		||||
                for (f in incoming)
 | 
			
		||||
                    if (f is Frame.Binary) a.receiveFrame(f.data)
 | 
			
		||||
                // when we leave connection will be closed. So far we do not close the adapter,
 | 
			
		||||
                // should we?
 | 
			
		||||
                _connectionFlow.value = false
 | 
			
		||||
                deferredAdapter = CompletableDeferred()
 | 
			
		||||
            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 { reconnectFlow.collect { if (it == true) close() } }
 | 
			
		||||
                    deferredAdapter.complete(a)
 | 
			
		||||
                    for (f in incoming)
 | 
			
		||||
                        if (f is Frame.Binary) a.receiveFrame(f.data)
 | 
			
		||||
                    // when we leave connection will be closed. So far we do not close the adapter,
 | 
			
		||||
                    // should we?
 | 
			
		||||
                    _connectionFlow.value = false
 | 
			
		||||
                    deferredAdapter = CompletableDeferred()
 | 
			
		||||
                }
 | 
			
		||||
            }
 | 
			
		||||
        }
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
		Loading…
	
	
			
			x
			
			
		
	
		Reference in New Issue
	
	Block a user