trashcoder
03/16/2022, 10:50 PMmaybe
, single
and completable
, i ended up creating my own observable. it seems to work but i would like to know, if this is the way that is meant to be used. especially in combination with coroutines.
(see thread for code)observable<String> { emitter ->
val executor = ioScheduler.newExecutor()
emitter.setDisposable(executor)
executor.submit {
GlobalScope.launch {
httpStatement.execute { httpResponse ->
val channel: ByteReadChannel = httpResponse.body()
while (!channel.isClosedForRead) {
channel.read {
emitter.onNext(it.decodeString())
}
}
}
}
}
}.subscribe {
println(it)
}
Arkadii Ivanov
03/17/2022, 9:51 AMObservable
from coroutine, you can try the code below:
callbackFlow {
while (isActive) {
delay(1000L) // Read data
send("abc") // Send data
}
}.asObservable() // Convert Flow to Observable
trashcoder
03/17/2022, 7:16 PMObservable
now.
As for the request:
I use KTOR 2.0.0 in my project as HTTP client. Now i need to read Server-Sent Events. I googled "KTOR SSE" and found this issue which then lead me here. So I copied this code and modified it a bit to make it work (at least I thought so ^^).
I mean, I can use my app normally (the UI is responsive and I can execute other actions on IO) and everytime the server sends an event (caused by another app) it appears in the console. So I do a one time request and get all the events. Isn't that "listening to the server"?
This is how I create the `httpStatement`:
val httpStatement = localClient.prepareGet("https://${connectionInfo.bridgeIP}/eventstream/clip/v2") {
applicationKeyHeader()
header("Accept", "text/event-stream")
}
Arkadii Ivanov
03/17/2022, 7:25 PMtrashcoder
03/17/2022, 7:38 PMcurl --insecure -N -H 'hue-application-key: <appkey>' -H 'Accept: text/event-stream' https://<ipaddress>/eventstream/clip/v2
So maybe i should really rethink this approach... 🙈Arkadii Ivanov
03/17/2022, 8:15 PMtext/event-stream
header and the server will stream events back. https://developer.mozilla.org/en-US/docs/Web/API/Server-sent_events/Using_server-sent_eventstrashcoder
03/17/2022, 8:39 PM