I'm using play framework, to generate chunked response. The code is:
class Test extends Controller {
public static void chunk() throws InterruptedException {
for (int i = 0; i < 10; i++) {
String data = repeat("" + i, 1000);
response.writeChunk(data);
Thread.sleep(1000);
}
}
}
When I use browser to visit http://localhost:9000/test/chunk
, I can see the data displayed increased every second. But, when I write a javascript function to receive and handle the data, found it will block until all data received.
The code is:
$(function(){
$.ajax(
"/test/chunked",
{
"success": function(data, textStatus, xhr) {
alert(textStatus);
}
}
);
});
I can see a message box popped up after 10s, when all the data received.
How to get the stream and handle the data in time?
Question&Answers:
os 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…