有没有办法将数据从网络服务器推送到浏览器?

发布于 2024-07-04 09:31:57 字数 107 浏览 5 评论 0原文

我当然知道Ajax,但Ajax的问题​​是浏览器要频繁轮询服务器以查找是否有新数据。 这会增加服务器负载。

除了频繁轮询服务器之外,还有其他更好的方法(甚至使用 Ajax)吗?

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.

Is there any better method (even using Ajax) other than polling the server frequently?

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

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

发布评论

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

评论(18

诠释孤独 2024-07-11 09:31:57

可能还想看看 ReverseHTTP

Might want to look at ReverseHTTP also.

初与友歌 2024-07-11 09:31:57

如果您使用的是 jsp 页面,您还可以查看 Java Pushlets

You can also look into Java Pushlets if you are using jsp pages.

趴在窗边数星星i 2024-07-11 09:31:57

请检查此库 https://github.com/SignalR/SignalR 了解如何将数据推送到客户端当它变得可用时动态地

please check this library https://github.com/SignalR/SignalR to know how to push data to clients dynamically as it becomes available

酒儿 2024-07-11 09:31:57

您可以尝试我们的Comet 组件 - 尽管它非常 实验性的...!

You could try out our Comet Component - though it's extremely experimental...!

无名指的心愿 2024-07-11 09:31:57

一旦打开到服务器的连接,它就可以保持打开状态,并且服务器可以推送内容很久以前我使用multipart/x-mixed-replace,但这在 IE 中不起作用。

我认为你可以通过轮询做一些聪明的事情,通过不发送未更改的标头内容但保持连接打开,使其更像推送,但我从未这样做过。

Once a connection is opened to the server it can be kept open and the server can Push content a long while ago I did with using multipart/x-mixed-replace but this didn't work in IE.

I think you can do clever stuff with polling that makes it work more like push by not sending content unchanged headers but leaving the connection open but I've never done this.

依 靠 2024-07-11 09:31:57

可能值得查看 Meteor Server,它是专为 COMET 设计的 Web 服务器。 不错的演示,它也被twitterfall

Might be worth checking out Meteor Server which is a web server designed for COMET. Nice demo and it also is used by twitterfall.

苏璃陌 2024-07-11 09:31:57

通过使用持久 http 连接可以实现您的目标。

查看维基百科上的 Comet 文章,这是一个很好的起点。

您没有提供太多信息,但如果您正在考虑构建某种事件驱动的网站(a'la digg sp)或类似的东西,您可能会考虑实现一个连接到的隐藏 IFRAME连接永远不会关闭的 url,然后您将脚本标签从服务器推送到客户端以执行更新。

It's possible to achive what you're aiming at through the use of persistent http connections.

Check out the Comet article over at wikipedia, that's a good place to start.

You're not providing much info but if you're looking at building some kind of event-driven site (a'la digg spy) or something along the lines of that you'll probably be looking at implementing a hidden IFRAME that connects to a url where the connection never closes and then you'll push script-tags from the server to the client in order to perform the updates.

來不及說愛妳 2024-07-11 09:31:57

您可以在客户端上使用 Flash/Flex 应用程序,在服务器端使用 BlazeDS 或 LiveCycle。 可以使用 RTMP 连接将数据推送到客户端。 请注意,RTMP 使用非标准端口。 但如果端口被阻止,您可以轻松地退回到轮询。

You can use a Flash/Flex application on the client with BlazeDS or LiveCycle on the server side. Data can be pushed to the client using an RTMP connection. Be aware that RTMP uses a non standard port. But you can easily fall back to polling if the port is blocked.

淡淡的优雅 2024-07-11 09:31:57

还有其他方法。 不确定他们在你的情况下是否“更好”。 您可以有一个 Java 小程序,它在页面加载时连接到服务器并等待服务器发送内容。 它的启动速度会慢一些,但允许浏览器不频繁地从服务器接收数据,而无需轮询。

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.

紫竹語嫣☆ 2024-07-11 09:31:57

Comet 的一个有趣的替代方案是在 Flash 中使用套接字。

An interesting alternative to Comet is to use sockets in Flash.

好菇凉咱不稀罕他 2024-07-11 09:31:57

Comet 实际上是由 Dojo Toolkit ( http://www.dojotoolkit.org ) 的 Alex Russell 创造的。 以下是更多信息的链接 http://cometdproject.dojotoolkit.org/

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/

我为君王 2024-07-11 09:31:57

我强烈建议在 Comet 上投入一些时间,但我不知道您可以使用的实际实现或库。

对于一种网络应用程序的“呼叫中心控制面板”,涉及更新实时呼叫中心的代理和呼叫队列状态,我们开发了一个可以工作的内部解决方案,但距离您可以使用的库还很远。

我们所做的是在服务器上实现一个小型服务,该服务与电话系统对话,等待新事件并维护情况照片。 该服务提供了一个小型网络服务器。

我们的网络客户端通过 HTTP 连接到该网络服务器,并请求最后一张照片(以 XML 编码),显示它,然后再次请求新照片。 此时,网络服务器可以:

  • 返回新照片,如果有的话
  • 阻止客户端几秒钟(在我们的设置中为 30 秒),等待某个事件发生并更改照片。 如果此时没有生成任何事件,它将返回相同的照片,只是为了允许连接保持活动状态,而不会使客户端超时。

这样,当客户端轮询时,它最多会在 0 到 30 秒内获得响应。 如果已经生成了新事件,它将立即获取它),否则它将阻塞,直到生成新事件。

它基本上是轮询,但它有点智能轮询,以免网络服务器过热。 如果 Comet 不是您的答案,我确信这可以使用相同的想法来实现,但使用更广泛的 AJAX 或 JSON 编码以获得更好的结果。 这是 AJAX 时代之前设计的,因此还有很大的改进空间。

如果有人可以提供一个实际的轻量级实现,那就太好了!

I would strongly suggest to invest some time on Comet, but I dont know an actual implementation or library you could use.

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.

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:

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

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.

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.

If someone can provide a actual lightweight implementation of this, great!

爱情眠于流年 2024-07-11 09:31:57

彗星绝对是你想要的。 根据您的语言/框架要求,有不同的服务器库可用。 例如,WebSync 是面向 ASP.NET/C#/IIS 开发人员的 IIS 集成 Comet 服务器,并且如果您需要与其他语言更紧密的集成,还有一堆其他独立服务器。

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.

一影成城 2024-07-11 09:31:57

看看 Comet(对 Ajax 是清洁剂这一事实的恶搞,Comet 也是),它基本上是“反向 Ajax”。 请注意,这需要每个用户都有长期的服务器连接才能接收通知,因此在编写应用程序时请注意性能影响。

http://en.wikipedia.org/wiki/Comet_(programming)

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.

http://en.wikipedia.org/wiki/Comet_(programming)

朕就是辣么酷 2024-07-11 09:31:57

是的,它称为反向 AjaxComet。 Comet 基本上是打开长期 HTTP 请求以将数据实时推送到 Web 浏览器的不同方式的总称。 我推荐StreamHub Push Server,他们有一些很酷的演示,而且更容易上手比任何其他服务器。 查看Comet 和 StreamHub 入门教程 快速介绍。 您可以使用社区版,该版可免费下载,但并发用户数限制为 20 个。 仅就支持而言,商业版本就非常值得,而且您还可以获得 SSL 和桌面 .NET 和 .NET 版本。 Java 客户端适配器。 您可以通过 Google 网上论坛获取帮助,这里有很多帮助网上有很多教程,还有 GWT Comet 适配器

Yes, it's called Reverse Ajax or 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 Tutorial for 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 adapter too.

温柔戏命师 2024-07-11 09:31:57

现在你应该使用 WebSockets。
这是 2011 年的标准,允许使用 HTTP 发起连接,然后将其升级为基于双向客户端-服务器消息的通信。

您可以轻松地从 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
};

服务器端处理取决于您的技术堆栈。

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.

You can easily initiate the connection from 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.

云归处 2024-07-11 09:31:57

是的,您要找的是 COMET http://en.wikipedia.org/wiki/ Comet_(编程)。 其他值得搜索的 Google 术语是 AJAX-push 和 reverse-ajax。

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.

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