访问使用AJAX或WebSockets的部分反应? [英] Accessing partial response using AJAX or WebSockets?

查看:123
本文介绍了访问使用AJAX或WebSockets的部分反应?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用的是一些客户端的JavaScript code通过一个HTTP GET从web服务器拉了很多的JSON数据。数据量可大,说50 MB。这是一个局域网,所以它不是太大的问题,但它仍然需要十秒钟左右。

为了使我的界面更加敏感,我想处理的数据块的响应,显示在用户界面中的数据,一旦它成为可用(比方说,每MB或每秒)。浏览器兼容性是不是一个问题;只要它适用于最新的Chrome和Firefox它会好起来的。但是,我不能修改服务器code。

是否有可能做到这一点,使用XMLHtt prequest或WebSockets的或者我还没有听说过一些其他的技术?

XMLHtt prequest.responseText 不明确的空当状态是加载

  

这个responseText属性必须返回运行这些步骤的结果是:

     
      
  1. 如果状态没有加载或DONE返回空字符串并终止这些步骤。
  2.   
  3. 返回文本响应实体主体。
  4.   

不过,我想缓冲将发生在沿途各个阶段,所以如果我设置一个计时器,定期轮询将它的工作的responseText

据我所知,的WebSockets需要在服务器端一个特殊的协议一样,所以这些都出来了。

限制:我不能修改服务器code

解决方案

通缉张贴此作为注释,但注释文本区域是有点限制性

服务器是否在此刻发送块中的数据或者是一个连续的数据流?如果你添加一个处理程序的 xmlHtt prequestInstance.onreadystatechange 多久就被触发?如果 xmlHtt prequestInstance.readystate 有3数值,然后就可以得到 xmlHtt prequestInstance.responseText <当前值/ code>并保持轨道的长度。下一次readyState的改变,你可以让所有的新数据开始在这一点上获得的最新数据。

我没有测试以下,但希望它很明确的:

  VAR XHR =新XMLHtt prequest();
变种lastPos = 0;
xhr.onreadystatechange =功能(){
  如果(xhr.readystate === 3){
    VAR数据= xhr.responseText.substring(lastPos);
    lastPos = xhr.responseText.length;

    处理(数据);
  }
};
当然//然后做`open`和`send` :)
 

这当然依赖于的onreadystatechange 事件射击。这将工作在IE浏览器,Chrome浏览器,Safari和Firefox浏览器。

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.

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.

Is it possible to do this, using XMLHttpRequest or WebSockets or some other technology I haven't heard of?

XMLHttpRequest.responseText is not explicitly empty while the state is LOADING:

The responseText attribute must return the result of running these steps:

  1. If the state is not LOADING or DONE return the empty string and terminate these steps.
  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?

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.

解决方案

Wanted to post this as a comment but the comment textarea is a bit restrictive

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

This of course relies on the onreadystatechange event firing. This will work in IE, Chrome, Safari and Firefox.

这篇关于访问使用AJAX或WebSockets的部分反应?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆