I'm trying to do server-sent events in Jester.
What I have now is this:
router index:
get "/time":
let data =
"retry: 3000\n" &
"data: {time: \'" & getClockStr() & "\'} \n\n"
resp(data, "text/event-stream")
It's working because browser is reconnecting every 3 seconds and getting new data.
But in principle, the server is supposed to keep the existing connection open and send more data through it.
# get "/live":
# await response.sendHeaders()
# for i in 0 .. 10:
# await response.send("The number is: " & $i & "</br>")
# await sleepAsync(1000)
# response.client.close()
Is there any way to get response object inside route? Or is there another approach?
Thanks! Ended up with this:
import nativesockets, httpbeast
…
get "/time-live":
request.sendHeaders(Http200,@({"Content-Type": "text/event-stream"}))
for i in 0 .. 10:
let data =
"retry: 3000\n" &
"data: {time: \'" & getClockStr() & "\'} \n\n"
request.send(data)
await sleepAsync(1000)
let nativeReq = request.getNativeReq()
nativeReq.forget()
nativeReq.client.close()
result.matched=true
break routesList
NativeRequest-agnostic close would appreciated in a PR :)
I'll look into it 😉