在AJAX增量服务器响应处理(在JavaScript) [英] Dealing with incremental server response in AJAX (in JavaScript)

查看:152
本文介绍了在AJAX增量服务器响应处理(在JavaScript)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有AJAX的应用程序,它基于服务器响应更新页面。该AJAX服务器响应基于该命令需要很长时间才能产生完整的响应,但它发出的部分信息,只要它的计算方法。这部分缓解/部分信息是发送突发,以及时间和每个突发的大小是联合国predictable。 CGI脚本(在Perl),其流命令输出到Web浏览器(以AJAX请求)的自动冲洗开启。

服务器响应基于外部命令的输出。而时间CMD>的/ dev / null的给出附近,10.0秒的平均值,时间CMD |头>的/ dev / null的'给出小于0.1秒(例如数据)的所有数据是单个呼叫结果给该外部命令。

情况如下所示的(ASCII艺术图如下)的:

 客户端| |服务器
--------- ---------

请求 -\
          \
           \
            \
             \  - >

             /- 响应
            /。
           /。
          / /  - 。
       <  -  / /。
           /。
          /  /-  [结束]
       <  -  / /
           /
          /
       <  -  /
 

我对这个问题的几个问题。

注意:服务器端是作为在Pe​​rl CGI脚本,我会preFER看到(也)解决方案,而无需使用JavaScript库/框架,像jQuery

  • 使用AJAX的应用程序的服务器端命令的输出线为主。每组行,开始有一个定义类型的线,并与其他类型的行结束,包括无关!和不可改变的数据。如果我只是流响应从命令为text / plain的,做加工的JavaScript在客户端,或者我应该在服务器pre-处理数据和发送数据的整个数据块为JSON使用应用/ JSON MIMETYPE?

  • 它可能会发生大数据块发送一次由服务器很快其次是另一个数据块。如何处理的情况时,的onreadystatechange 处理程序被调用时previous调用没有完成的工作?我应该使用全局变量,信号量,或通过状态变量的处理参数(当然,使用 xhr.onreadystatechange =功能(){的handleRequest(XHR,州)} )<? / P>

  • 我应该使用'text / plain的或应用/ JSON的,或者多部分/ x0mixed替换这个?注意:这个应用程序应该在(alomst)任何浏览器

  • 如何应对网络浏览器(JavaScript引擎),它只有在收到完整的响应之后,调用onreadystatechange的(所以我没有看到 xhr.readyState == 3 即部分响应不止一次)?好了,旁边用一些JavaScript框架。

  • 如何处理不完整的响应(在这种情况下,意味着不完整的线)。

  • 我应该发送响应结束标记,还是要靠办理登机手续,如果我们收到的所有数据,或者我可以简单地依赖于检测 xhr.readyState == 4

即使部分反应将是有益的。

解决方案

我觉得彗星是什么,你需要为你的解决方案的一部分。你还可以(如果我说对了)检出贝叶协议这是实施由道场基金会。 整个事情还是很新的(尽管一些有可能与第一HTML5实现)。

此外,你可能必须要实现轮询方法。另一个问题是,有多少数据在客户端的JavaScript跨preTER可以处理。是否有你以某种方式页你的数据,让你不会有请求处理的问题还在处理中,而aother响应进来就任何可能性?

I have AJAX app which updates page based on server response. The command that AJAX server response is based on takes long time to generate full response, but it sends partial information as soon as it is calculated. This partial response / partial info is send in "burst", and time and size of each burst is unpredictable. CGI script (in Perl) that streams command output to web browser (to AJAX request) has autoflush turned on.

The server response is based on output of external command. While 'time cmd > /dev/null" gives around 10.0 seconds on average, 'time cmd | head > /dev/null' gives less than 0.1 seconds (for example data). All data is result of single call to this external command.

The situation looks like the following (ASCII-art diagram follows):

 client |     | server
---------     ---------

request -\
          \
           \
            \
             \->

             /- response
            /      .
           /       .
          /  /-    .
       <-/  /      . 
           /       .
          /  /-  [end]
       <-/  /
           /
          /
       <-/

I have a few questions about this problem.

Note: server side is done as CGI script in Perl, and I would prefer to see (also) solution without using JavaScript library / framework like jQuery.

  • The output of command used by server side of AJAX app is line based. Each group of lines, beginning with one defined kind of line, and ending with other kind of line, consist of independend and unchangeable data. Should I just stream response from a command as 'text/plain' and do processing in JavaScript on client side, or should I pre-process data on server, and send whole chunks of data as JSON using 'application/json' mimetype?

  • It might happen that large chunk of data send at once by server is followed soon by another chunk of data. How to deal with situation when onreadystatechange handler is invoked while previous invocation didn't finished work? Should I use global variable as semaphore, or pass state variable as handler parameter (well, use xhr.onreadystatechange = function() { handleRequest(xhr, state) })?

  • Should I use 'text/plain' or 'application/json', or perhaps 'multipart/x0mixed-replace' for this? Note: this app should work in (alomst) any browser.

  • How to deal with web browser (JavaScript engines) which invoke onReadyStateChange only after receiving complete response (so I don't see xhr.readyState == 3 i.e. partial response more than once)? Well, beside using some JavaScript framework.

  • How to deal with incomplete responses (which in this situation mean incomplete lines).

  • Should I send end of response marker, or rely on counter to check if we received all data, or can I simply rely on detecting xhr.readyState == 4?

Even partial response would be helpful.

解决方案

I think Comet is part of what you need for your solution. You can additionally (if I got that right) checkout the Bayeux Protocol which was implemented by the Dojo Foundation. The whole thing is still very new (though some of it might be possible with the first HTML5 implementations).

Besides that you would probably have to implement the polling approach. The other question is, how much data the client side JavaScript interpreter can handle. Is there any possibility for you to somehow "page" your data so that you won't have the problem of request handling still processing while aother response comes in already?

这篇关于在AJAX增量服务器响应处理(在JavaScript)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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