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

发布于 2024-10-08 21:53:46 字数 838 浏览 0 评论 0原文

我正在编写一个小型演示网络服务。

我想使用此 Web 服务向服务使用者提供定期更新。

我必须指出,我只对高层(即 HTTP 和 Javascript)感兴趣。我不关心 HTTP 命令或动态 javascript 是如何生成的。我对该机制的底层概念感兴趣,并且不想束缚于特定的实现语言(我可以决定使用 C/C++、PHP、Python、C#、Java、Ruby 甚至 Lua,所以最好离开语言的具体方面)。

这是典型用例场景中的事件序列:

  • 消费者首先通过 AJAX HTTP GET 命令发出初始请求

  • 服务以第一组数据响应

  • 随机时间后,服务向客户端发送更多数据

  • 如果没有更多可用数据,服务将最后的数据发送到客户端并告诉客户端有效地停止“轮询”或“侦听”新数据到达

IIRC,所需的行为(如上所述)可以通过以下方式实现:

  1. 在页面中插入隐藏框架服务使用者
  2. 从服务器生成 JSON 响应,其中包含最新数据以及插入隐藏框架的动态 Javascript。客户端的 javascript '轮询'服务以获取新数据
  3. 来自服务的最终数据不再包含动态 Javascript,从而有效地阻止客户端连续轮询服务

有人可以解释实现该行为所需的正确(最佳实践)步骤吗如上所述?

[编辑]

正如您可能意识到的,这是一种(轻量级)服务器推送形式。我知道 Comet 等,但它们对于我的需求来说太笨重/沉重(而且它们需要额外的第三方软件等)。我只想使用 Javascript 和 HTTP 推出我自己的轻量级版本。

I am writing a small demo web service.

I want to use this web service to provide periodic updates to the service consumer.

I must point out that I am interested only in the high level layer (i.e. HTTP and Javascript). I am not concerned with how the HTTP commands or dynamic javascript is generated. 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).

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

  • 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:

  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. The javascript on the clientside 'polls' the service for new data
  3. final data from the service no longer contains the dynamic Javascript, thereby effectively stopping the client from continuously polling the service

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). I just want to roll my own lightweight version using Javascript and HTTP.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

无声无音无过去 2024-10-15 21:53:46

我想你想调查Comet“推送技术”和“服务器推送”是此类事物的更广泛术语。

I think you want to investigate Comet. "Push technology" and "server push" are broader terms for this kind of thing.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文