使用 AJAX 或 WebSockets 访问部分响应? [英] Accessing partial response using AJAX or WebSockets?
问题描述
我正在使用一些客户端 JavaScript 代码通过 HTTP GET 从网络服务器中提取大量 JSON 数据.数据量可能很大,比如 50 MB.这是在局域网上,所以问题不大,但仍然需要十秒钟左右.
I'm using some client-side JavaScript code to pull a lot of JSON data from a webserver via an HTTP GET. The amount of data can be big, say 50 MB. This is on a LAN so it's not much of a problem, but it still takes ten seconds or so.
为了使我的界面更具响应性,我想以块的形式处理响应,一旦数据可用就在 UI 中显示数据(比如说,每 MB 或每秒).浏览器兼容性不是问题;只要它适用于最新的 Chrome 和 Firefox,就可以了.但是,我无法修改服务器代码.
To make my interface more responsive, I would like to process the response in chunks, showing data in the UI as soon as it becomes available (let's say, per MB or per second). Browser compatibility is not an issue; as long as it works on the latest Chrome and Firefox it'll be okay. However, I cannot modify the server code.
是否可以使用 XMLHttpRequest 或 WebSockets 或其他一些我没有听说过的技术来做到这一点?
Is it possible to do this, using XMLHttpRequest or WebSockets or some other technology I haven't heard of?
XMLHttpRequest.responseText
在状态为 LOADING
时不明确为空:
XMLHttpRequest.responseText
is not explicitly empty while the state is LOADING
:
responseText 属性必须返回运行这些步骤的结果:
The responseText attribute must return the result of running these steps:
- 如果状态不是 LOADING 或 DONE,则返回空字符串并终止这些步骤.
- 返回文本响应实体正文.
但我认为缓冲会在整个过程的各个阶段发生,所以如果我设置一个计时器来定期轮询 responseText
会起作用吗?
But I suppose buffering will happen at various stages along the way, so will it work if I set a timer to periodically poll responseText
?
据我所知,WebSockets 在服务器端也需要一个特殊的协议,所以这些都没有了.
As far as I can tell, WebSockets require a special protocol on the server side as well, so those are out.
限制:我不能修改服务器代码.
Restriction: I cannot modify the server code.
推荐答案
想将此作为评论发布,但评论 textarea 有点限制
服务器现在是分块发送数据还是一个连续的流?如果向 xmlHttpRequestInstance.onreadystatechange
添加处理程序,它多久触发一次?如果 xmlHttpRequestInstance.readystate
的值为 3,那么您可以获取 xmlHttpRequestInstance.responseText
的当前值并跟踪长度.下次就绪状态更改时,您可以通过从该点开始获取所有新数据来获取最新数据.
Does the server send the data in chunks at the moment or is it one continuous stream? If you add a handler to the xmlHttpRequestInstance.onreadystatechange
how often does it get triggered? If the xmlHttpRequestInstance.readystate
has a value of 3 then you can get the current value of the xmlHttpRequestInstance.responseText
and keep a track of the length. The next time the readystate changes you can get the most recent data by getting all new data starting at that point.
我还没有测试以下内容,但希望它足够清楚:
I've not tested the following but hopefully it's clear enough:
var xhr = new XMLHttpRequest();
var lastPos = 0;
xhr.onreadystatechange = function() {
if(xhr.readystate === 3) {
var data = xhr.responseText.substring(lastPos);
lastPos = xhr.responseText.length;
process(data);
}
};
// then of course do `open` and `send` :)
这当然依赖于 onreadystatechange
事件触发.这将适用于 IE、Chrome、Safari 和 Firefox.
This of course relies on the onreadystatechange
event firing. This will work in IE, Chrome, Safari and Firefox.
这篇关于使用 AJAX 或 WebSockets 访问部分响应?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!