[WEBrick] streaming output

D

Dan Janowski

Is there a way to send a stream of output while processing, I want to
send ongoing status? The basic response construct seems to want the
output in a finalized form. The clients will render progressively from
what I understand.

Dan
 
G

GOTOU Yuuzou

In message said:
Is there a way to send a stream of output while processing, I want to
send ongoing status? The basic response construct seems to want the
output in a finalized form. The clients will render progressively from
what I understand.

Set an IO to res.body, and write data from another thread.

require "webrick"

class Streamlet < WEBrick::HTTPServlet::AbstractServlet
def do_GET(req, res)
res["content-type"] = "application/octet-stream"
r, w = IO.pipe
res.body = r
Thread.start{
10.times{|i|
w.write("#{i}" * 4096)
sleep(1)
}
w.close
}
end
end

httpd = WEBrick::HTTPServer.new:)Port=>10080)
httpd.mount("/", Streamlet)
trap:)INT){ httpd.shutdown }
httpd.start
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,015
Latest member
AmbrosePal

Latest Threads

Top