Sourabh Rawat
12/07/2020, 9:43 AMfun openWebSocket(scope: CoroutineScope = GlobalScope, consumer: JsonRpcListener) {
scope.launch {
<http://httpClient.ws|httpClient.ws>(method = HttpMethod.Get, host = host, port = port, path = path) {
incoming.consumeAsFlow()
.onEach { consumer.run(it) }
.collect()
}
}
}
So how do I close the ws from client side?e5l
12/07/2020, 9:45 AMcollect()
, you can try:
launch {
delay(....) // or suspend for some condition
outgoing.close()
}
before collect, or run collect()
in launch
and close outgoing
in the origin coroutineSourabh Rawat
12/07/2020, 9:49 AMoutgoing
is available in origin coroutine. Assuming outgoing
means SendChannel<Frame>
for ws sessione5l
12/07/2020, 9:50 AMoutgoing
should be available on the same scope as incoming
Sourabh Rawat
12/07/2020, 9:57 AMfun openWebSocket(scope: CoroutineScope = GlobalScope, listener: JsonRpcListener): () -> Unit {
val job = scope.launch {
<http://httpClient.ws|httpClient.ws>(method = HttpMethod.Get, host = host, port = port, path = path) {
incoming.consumeAsFlow()
.onEach { listener(it) }
.collect()
}
}
return {
job.cancel()
}
}
So i tried this, using return object (can be renamed to RemoveListener
or something)? Is it a proper way tho?