简体   繁体   English

使用AJAX或WebSockets访问部分响应?

[英]Accessing partial response using AJAX or WebSockets?

I'm using some client-side JavaScript code to pull a lot of JSON data from a webserver via an HTTP GET. 我正在使用一些客户端JavaScript代码通过HTTP GET从Web服务器中提取大量JSON数据。 The amount of data can be big, say 50 MB. 数据量可能很大,比如50 MB。 This is on a LAN so it's not much of a problem, but it still takes ten seconds or so. 这是在局域网上,所以这不是一个问题,但它仍然需要十秒钟左右。

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). 为了使我的界面更具响应性,我想以块的形式处理响应,一旦可用就显示UI中的数据(比方说,每MB或每秒)。 Browser compatibility is not an issue; 浏览器兼容性不是问题; as long as it works on the latest Chrome and Firefox it'll be okay. 只要它适用于最新的Chrome和Firefox,它就没问题。 However, I cannot modify the server code. 但是,我无法修改服务器代码。

Is it possible to do this, using XMLHttpRequest or WebSockets or some other technology I haven't heard of? 是否可以使用XMLHttpRequest或WebSockets或其他一些我没有听说过的技术来做到这一点?

XMLHttpRequest.responseText is not explicitly empty while the state is LOADING : 当状态为LOADING时, XMLHttpRequest.responseText未显式为空:

The responseText attribute must return the result of running these steps: responseText属性必须返回运行这些步骤的结果:

  1. If the state is not LOADING or DONE return the empty string and terminate these steps. 如果状态不是LOADING或DONE返回空字符串并终止这些步骤。
  2. Return the text response entity body. 返回文本响应实体主体。

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 ? 但是我认为缓冲会在整个过程中的各个阶段发生,所以如果我设置一个定时器来定期轮询responseText ,它会起作用吗?

As far as I can tell, WebSockets require a special protocol on the server side as well, so those are out. 据我所知,WebSockets也需要服务器端的特殊协议,所以这些协议已经完成。

Restriction: I cannot modify the server code. 限制:我无法修改服务器代码。

Wanted to post this as a comment but the comment textarea is a bit restrictive 想发布这个评论,但评论textarea有点限制

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? 如果你在xmlHttpRequestInstance.onreadystatechange添加一个处理程序,它的触发频率是多少? 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. 如果xmlHttpRequestInstance.readystate的值为3,那么您可以获取xmlHttpRequestInstance.responseText的当前值并跟踪长度。 The next time the readystate changes you can get the most recent data by getting all new data starting at that point. 下次readystate更改时,您可以通过从该点开始获取所有新数据来获取最新数据。

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` :)

This of course relies on the onreadystatechange event firing. 这当然依赖于onreadystatechange事件触发。 This will work in IE, Chrome, Safari and Firefox. 这适用于IE,Chrome,Safari和Firefox。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM