javascript 如何进行网页的实时更新?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/3864501/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-25 02:29:56  来源:igfitidea点击:

How does one do realtime updates of a web page?

javascript

提问by Billy ONeal

Google's GMail service does it because it integrates Google Talk -- and Etherpad (now typewith.me) made famous the system which is used by, for example, Google Wave. All such systems update the page the user is working on effectively instantly when other users make changes to the page. It's easy to tell the server that a change has happened when it has happened, but it's more difficult to get clients to update themselves.

谷歌的 GMail 服务之所以能这样做,是因为它集成了 Google Talk——而 Etherpad(现在是 typewith.me)使 Google Wave 等使用的系统闻名于世。当其他用户对页面进行更改时,所有此类系统都会立即有效地更新用户正在处理的页面。很容易在发生更改时告诉服务器发生了更改,但让客户端自行更新则比较困难。

How does this kind of realtime editing work? Does it simply have the client ping the server tens of times per second for updates?

这种实时编辑是如何工作的?它是否只是让客户端每秒 ping 服务器数十次以进行更新?

回答by SLaks

You can use Comet.

您可以使用彗星

回答by Gabe

I suggest using AJAX & jQuery for Asynchronous JS

我建议将 AJAX 和 jQuery 用于异步 JS

http://api.jquery.com/category/ajax/

http://api.jquery.com/category/ajax/

回答by samy

There are many options but basically i'd recommend you look into XMPP. i don't think i'm good enough to boil it down correctly, so i'll let a wiki talk for me

有很多选择,但基本上我建议您查看XMPP。我认为我不够好,无法正确地将其归结,所以我会让维基为我说话

In fact, google voice and video uses itfor these systems.

事实上,谷歌语音和视频将其用于这些系统。



About AJAX, i think it's a communication channel, not a platform or a protocol for multiple person exchange. You could also answer "Use xml over http!"and still be at the same point :)

关于 AJAX,我认为它是一个沟通渠道,而不是一个平台或多人交流的协议。你也可以回答"Use xml over http!"但仍然在同一点:)

回答by Ruel

Asynchronous JavaScript and XMLor AJAX

异步 JavaScript 和 XMLAJAX

With Ajax, web applications can retrieve data from the server asynchronously in the background without interfering with the display and behavior of the existing page. The use of Ajax techniques has led to an increase in interactive or dynamic interfaces on web pages. Data is usually retrieved using the XMLHttpRequest object. Despite the name, the use of XML is not actually required, nor do the requests need to be asynchronous.

使用 Ajax,Web 应用程序可以在后台从服务器异步检索数据,而不会干扰现有页面的显示和行为。Ajax 技术的使用导致网页上交互式或动态界面的增加。通常使用 XMLHttpRequest 对象检索数据。尽管名称如此,但实际上并不需要使用 XML,请求也不需要是异步的。

回答by Jas

Javascript / Ajax allows you to send code to be executed on the client-side (that means, by the browser). Now if you e.g. define a loop which checks for new messages on server every 5 seconds, you can update the web-page "in-real-time" (plus the time for the server to process the req and send response), or similar. A practical example would be the RoR Prototype periodically_call_remote Ajax helper.

Javascript / Ajax 允许您发送要在客户端(即浏览器)执行的代码。现在,如果你定义一个循环,每 5 秒检查一次服务器上的新消息,你可以“实时”更新网页(加上服​​务器处理请求和发送响应的时间),或类似的. 一个实际的例子是 RoR Prototypeperiodic_call_remote Ajax 助手。

Hope this helps!

希望这可以帮助!

回答by Ravindra Sane

As everyone says.. AJAX.

正如大家所说.. AJAX。

The client keeps on asking the server, after say 30 secs if there is anything new for it to do. Also, you can set the timeout value on an ajax request. keep the time out a bit high.. and the server replies whenever there is something new.

客户端不断询问服务器,在说 30 秒后是否有任何新的事情要做。此外,您可以在 ajax 请求上设置超时值。保持超时时间有点高......服务器会在有新东西时回复。

There is no way that the server can other wise ask the client to load some data.

服务器无法以其他方式要求客户端加载一些数据。

If you are thinking of implementing something on the same lines, look up strophe.js which is an XMPP js-library

如果您正在考虑在同一行上实现某些内容,请查找 strophe.js,它是一个 XMPP js 库

回答by oshirowanen

AFAIK, they use some form of AJAX. However, I would recommend you use the AJAX libraries via jQuery. AJAX is simplified a lot if you use jQuery to do it.

AFAIK,他们使用某种形式的 AJAX。但是,我建议您通过 jQuery 使用 AJAX 库。如果使用 jQuery,AJAX 会简化很多。