asad.awadia
02/20/2018, 4:09 PMlaunch { while(true) { //listen to keyboard input and do stuff } }
Jonathan
02/20/2018, 4:18 PMin
.bufferedReader()
.useLines { lines ->
lines.forEach { send(it) }
}
}
```fun InputStream.openReadChannel(): ReceiveChannel<String> = produce {
bufferedReader()
.useLines { lines ->
lines.forEach { send(it) }
}
}
fun main(args: Array<String>) {
val inputChannel = System.`in`.openReadChannel()
val printJob = launch {
println("Start to listen on inputs")
inputChannel.consumeEach { println("User entered: $it") }
println("Stop to listen on inputs")
}
runBlocking {
delay(10, TimeUnit.SECONDS)
inputChannel.cancel()
printJob.join()
}
}