Skip to content

Instantly share code, notes, and snippets.

@auryn31
Created July 19, 2019 09:27
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save auryn31/8bc0086aee94e7dc39d70bd6d6f08447 to your computer and use it in GitHub Desktop.
Save auryn31/8bc0086aee94e7dc39d70bd6d6f08447 to your computer and use it in GitHub Desktop.
class TimeHandler : Handler<RoutingContext> {
override fun handle(rtx: RoutingContext) {
val response = rtx.response()
response.setChunked(true)
// set headers
response.headers().add("Content-Type", "text/event-stream;charset=UTF-8")
response.headers().add("Connection", "keep-alive")
response.headers().add("Cache-Control", "no-cache")
response.headers().add("Access-Control-Allow-Origin", "*")
val flow = TimeProducer.instance.obs
// subscribe to the public timer observable
val disposal = flow.subscribe({
response.write(SseModel(data = "the current time is $it", event = "time").toString())
}, ::println, {
response.end()
})
// stop observing it the pipe is broken
response.closeHandler{
disposal.dispose()
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment