ajax 有什么方法可以将数据从 Web 服务器推送到浏览器?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/19995/
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
Is there some way to PUSH data from web server to browser?
提问by Niyaz
Of course I am aware of Ajax, but the problem with Ajax is that the browser should poll the server frequently to find whether there is new data. This increases server load.
Ajax我当然知道,但是Ajax的问题是浏览器要经常轮询服务器,看是否有新数据。这会增加服务器负载。
Is there any better method (even using Ajax) other than polling the server frequently?
除了频繁轮询服务器之外,还有没有更好的方法(甚至使用 Ajax)?
采纳答案by Greg Hurlman
Yes, what you're looking for is COMET http://en.wikipedia.org/wiki/Comet_(programming). Other good Google terms to search for are AJAX-push and reverse-ajax.
是的,您正在寻找的是 COMET http://en.wikipedia.org/wiki/Comet_(programming)。其他好用的 Google 搜索词是 AJAX-push 和 reverse-ajax。
回答by Nosrama
Yes, it's called Reverse Ajaxor Comet. Comet is basically an umbrella term for different ways of opening long-lived HTTP requests in order to push data in real-time to a web browser. I'd recommend StreamHub Push Server, they have some cool demos and it's much easier to get started with than any of the other servers. Check out the Getting Started with Comet and StreamHub Tutorialfor a quick intro. You can use the Community Edition which is available to download for free but is limited to 20 concurrent users. The commercial version is well worth it for the support alone plus you get SSL and Desktop .NET & Java client adapters. Help is available via the Google Group, there's a good bunch of tutorials on the net and there's a GWT Comet adaptertoo.
是的,它被称为Reverse Ajax或Comet。Comet 基本上是打开长期 HTTP 请求以将数据实时推送到 Web 浏览器的不同方式的总称。我推荐StreamHub Push Server,他们有一些很酷的演示,而且它比任何其他服务器都更容易上手。查看Comet 入门和 StreamHub 教程以获得快速介绍。您可以使用可免费下载的社区版,但仅限 20 个并发用户。商业版本非常值得仅提供支持,此外您还可以获得 SSL 和桌面 .NET 和 Java 客户端适配器。可通过Google Group获得帮助,网上有很多教程,还有一个GWT Comet 适配器。
回答by pafinde
Nowadays you should use WebSockets. This is 2011 standard that allows to initiate connections with HTTP and then upgrade them to two-directional client-server message-based communication.
现在你应该使用 WebSockets。这是 2011 年的标准,它允许使用 HTTP 发起连接,然后将它们升级为基于消息的双向客户端-服务器通信。
You can easily initiate the connection from javascript:
您可以轻松地从 javascript 启动连接:
var ws = new WebSocket("ws://your.domain.com/somePathIfYouNeed?args=any");
ws.onmessage = function (evt)
{
var message = evt.data;
//decode message (with JSON or something) and do the needed
};
The sever-side handling depend on your tenchnology stack.
服务器端处理取决于您的技术堆栈。
回答by Dan Herbert
Look into Comet (a spoof on the fact that Ajax is a cleaning agent and so is Comet) which is basically "reverse Ajax." Be aware that this requires a long-lived server connection for each user to receive notifications so be aware of the performance implications when writing your app.
看看 Comet(对 Ajax 是一种清洁剂,Comet 也是一种清洁剂这一事实的恶搞),它基本上是“反向 Ajax”。请注意,这需要每个用户都有一个长期的服务器连接才能接收通知,因此在编写应用程序时请注意性能影响。
回答by jvenema
Comet is definitely what you want. Depending on your language/framework requirements, there are different server libraries available. For example, WebSync is an IIS-integrated comet server for ASP.NET/C#/IIS developers, and there are a bunch of other standalone servers as well if you need tighter integration with other languages.
彗星绝对是你想要的。根据您的语言/框架要求,有不同的服务器库可用。例如,WebSync 是 IIS 集成的 Comet 服务器,供 ASP.NET/C#/IIS 开发人员使用,如果您需要与其他语言更紧密地集成,还有许多其他独立服务器。
回答by ???
Yet another, standard, way is SSE (Server-Sent Events, also known as EventSource, after the JavaScript object).
另一种标准方式是SSE(服务器发送的事件,也称为 EventSource,在 JavaScript 对象之后)。
回答by Gilles
An interesting alternative to Comet is to use sockets in Flash.
Comet 的一个有趣的替代方法是在 Flash 中使用套接字。
回答by Pablo Alsina
I would strongly suggest to invest some time on Comet, but I dont know an actual implementation or library you could use.
我强烈建议在 Comet 上投入一些时间,但我不知道您可以使用的实际实现或库。
For an sort of "callcenter control panel" of a web app that involved updating agent and call-queue status for a live Callcenter we developed an in-house solution that works, but is far away from a library you could use.
对于涉及更新实时呼叫中心的代理和呼叫队列状态的 Web 应用程序的某种“呼叫中心控制面板”,我们开发了一个有效的内部解决方案,但与您可以使用的库相距甚远。
What we did was to implement a small service on the server that talks to the phone-system, waits for new events and maintains a photograph of the situation. This service provides a small webserver.
我们所做的是在服务器上实现一个与电话系统对话的小服务,等待新事件并维护情况照片。该服务提供了一个小型网络服务器。
Our web-clients connects over HTTP to this webserver and ask for the last photo (coded in XML), displays it and then goes again, asking for the new photo. The webserver at this point can:
我们的网络客户端通过 HTTP 连接到这个网络服务器并请求最后一张照片(以 XML 编码),显示它然后再次访问,请求新照片。此时的网络服务器可以:
- Return the new photo, if there is one
- Block the client for some seconds (30 in our setup) waiting for some event to ocurr and change the photograph. If no event was generated at that point, it returns the same photo, only to allow the connection to stay alive and not timeout the client.
- 返回新照片,如果有的话
- 阻止客户端几秒钟(在我们的设置中为 30),等待某个事件发生并更改照片。如果此时没有生成事件,它会返回相同的照片,只允许连接保持活动状态并且不会使客户端超时。
This way, when clients polls, it get a response in 0 to 30 seconds max. If a new event was already generated it gets it immediately), otherwise it blocks until new event is generated.
这样,当客户端进行轮询时,最多可在 0 到 30 秒内得到响应。如果已经生成了新事件,则立即获取),否则会阻塞,直到生成新事件。
It's basically polling, but it somewhat smart polling to not overheat the webserver. If Comet is not your answer, I'm sure this could be implemented using the same idea but using more extensively AJAX or coding in JSON for better results. This was designed pre-AJAX era, so there are lots of room for improvement.
它基本上是轮询,但它有点聪明,不会使网络服务器过热。如果 Comet 不是您的答案,我相信这可以使用相同的想法来实现,但使用更广泛的 AJAX 或 JSON 编码以获得更好的结果。这是在 AJAX 时代之前设计的,因此还有很多改进的空间。
If someone can provide a actual lightweight implementation of this, great!
如果有人可以提供一个实际的轻量级实现,那就太好了!
回答by Mike Schall
Comet was actually coined by Alex Russell from Dojo Toolkit ( http://www.dojotoolkit.org). Here is a link to more infomration http://cometdproject.dojotoolkit.org/
Comet 实际上是由来自 Dojo Toolkit ( http://www.dojotoolkit.org) 的Alex Russell 创造的。这是更多信息的链接http://cometdproject.dojotoolkit.org/
回答by Kibbee
There are other methods. Not sure if they are "better" in your situation. You could have a Java applet that connects to the server on page load and waits for stuff to be sent by the server. It would be a quite a bit slower on start-up, but would allow the browser to receive data from the server on an infrequent basis, without polling.
还有其他方法。不确定它们在您的情况下是否“更好”。您可以有一个 Java 小程序,它在页面加载时连接到服务器并等待服务器发送内容。启动时会慢很多,但允许浏览器在不频繁的基础上从服务器接收数据,而无需轮询。

