Created
July 19, 2019 09:27
-
-
Save auryn31/8bc0086aee94e7dc39d70bd6d6f08447 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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