Skip to content

Instantly share code, notes, and snippets.

@f-mer

f-mer/exmaple.cr Secret

Created January 20, 2018 18:58
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 f-mer/92dbf27acb2bdfd0fc4014210a34d3bb to your computer and use it in GitHub Desktop.
Save f-mer/92dbf27acb2bdfd0fc4014210a34d3bb to your computer and use it in GitHub Desktop.
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