-
-
Save f-mer/92dbf27acb2bdfd0fc4014210a34d3bb 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
def call(req : HTTP::Request, res : HTTP::Server::Response) | |
HTTP::Client.get("/logs") do |log_res| | |
stream(res, log_res.body_io) do |r, w| | |
r.each_line do |line| | |
w.puts "data: #{line}\n\n" | |
w.flush | |
end | |
end | |
end | |
puts "never gets executed" | |
end | |
def stream(res, io, heartbeat = 5.seconds, &blk : (IO, IO) ->) | |
res.not_nil!.headers["Content-Type"] = "text/event-stream" | |
spawn do | |
blk.call(io, res) | |
end | |
while !res.closed? | |
res.puts ";ok\n\n" | |
res.flush | |
sleep heartbeat | |
end | |
rescue | |
io.close | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment