Buffering in WEBrick

B

Bill Atkins

I'm writing a web application with WEBrick that occasionally has to
send large streams of data to the browser. However, WEBrick seems to
be storing all of the content and then sending it to the user all at
once. Is there any way I could periodically flush WEBrick's output so
the user doesn't have to stare at a blank screen while the server is
working?

Bill Atkins
 
G

GOTOU Yuuzou

Hi, sorry for late reply.

In message said:
I'm writing a web application with WEBrick that occasionally has to
send large streams of data to the browser. However, WEBrick seems to
be storing all of the content and then sending it to the user all at
once. Is there any way I could periodically flush WEBrick's output so
the user doesn't have to stare at a blank screen while the server is
working?

If the body of response is IO, its output will be flushed
with every reading of 4096 bytes chunk.

require "webrick"

class Streamlet < WEBrick::HTTPServlet::AbstractServlet
def do_GET(req, res)
res["content-type"] = "text/plain"
reader, writer = IO.pipe
Thread.start{
10000.times{|i|
puts i
writer << "a" * 1000
writer << "\n"
}
writer.close
}
res.body = reader
end
end

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

regards,
 

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
474,149
Messages
2,570,842
Members
47,388
Latest member
EarthaGilm

Latest Threads

Top