简体   繁体   English

如何从RESTful Web服务提供AJAX定期更新?

[英]how to provide AJAX periodic updates from a RESTful web service?

I am writing a small demo web service. 我正在编写一个小型演示Web服务。

I want to use this web service to provide periodic updates to the service consumer. 我想使用此Web服务向服务使用者提供定期更新。

I must point out that I am interested only in the high level layer (ie HTTP and Javascript). 我必须指出,我只对高层(即HTTP和Javascript)感兴趣。 I am not concerned with how the HTTP commands or dynamic javascript is generated. 我不关心HTTP命令或动态javascript的生成方式。 I am interested in the underlying concepts of the mechanism, and do not want to get tied into a particular implementation language (I could decide to use C/C++, PHP, Python, C#, Java, Ruby or even Lua so its best to leave the language specific aspects out). 我对这种机制的基本概念感兴趣,并且不想被某种特定的实现语言所束缚(我可以决定使用C / C ++,PHP,Python,C#,Java,Ruby甚至是Lua,所以最好离开语言特定方面)。

This is the sequence of events in a typical use case scenario: 这是典型用例场景中的事件序列:

  • The consumer first makes an intial request via an AJAX HTTP GET command 使用者首先通过AJAX HTTP GET命令发出初始请求

  • the service responds with first set of data 服务以第一组数据响应

  • after a random time, the service sends more data to the client 随机时间过后,服务会向客户端发送更多数据

  • if no more data available, the service sends the last data to the client and tell the client to effectively, stop 'polling' or 'listening' for new data arrivals 如果没有更多可用数据,则该服务会将最后一个数据发送给客户端,并告诉客户端有效地停止“轮询”或“监听”新数据

IIRC, the required behavior (as described above), can be achieved by: IIRC,所需的行为(如上所述)可以通过以下方式实现:

  1. inserting a hidden frame in the page of the service consumer 在服务使用者的页面中插入隐藏的框架
  2. generating a JSON response from the server which contains both the latest data, as well as dynamic Javascript which is inserted into the hidden frame. 从服务器生成一个JSON响应,其中包含最新数据以及插入隐藏帧中的动态Javascript。 The javascript on the clientside 'polls' the service for new data 客户端上的javascript“轮询”服务以获取新数据
  3. final data from the service no longer contains the dynamic Javascript, thereby effectively stopping the client from continuously polling the service 服务中的最终数据不再包含动态Javascript,从而有效地阻止了客户端连续轮询服务

Can someone explain the correct (best practices) steps required to implement the behavior described above? 有人可以解释实现上述行为所需的正确(最佳实践)步骤吗?

[Edit] [编辑]

As you may realize, this is a form of (lightweight) server push. 如您所知,这是(轻量级)服务器推送的一种形式。 I am aware of Comet etc, but they are too clunky/heavy for my needs (plus they require additional third party software etc). 我知道Comet等,但是对于我的需求,它们太笨拙/沉重(此外,它们还需要其他第三方软件等)。 I just want to roll my own lightweight version using Javascript and HTTP. 我只想使用Javascript和HTTP投放自己的轻量级版本。

I think you want to investigate Comet . 我认为您想调查彗星 "Push technology" and "server push" are broader terms for this kind of thing. “推送技术”和“服务器推送”是这类事情的广义术语。

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

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