异步servlet长轮询和贝叶协议(Comet)之间的区别 [英] Difference between async servlet long poll and bayeux protocol (Comet)

查看:223
本文介绍了异步servlet长轮询和贝叶协议(Comet)之间的区别的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

简单的异步servlet和Comet/Bayeux协议有什么区别?

我正在尝试实现一种服务器推送"(或反向Ajax")类型的网页,该网页将在服务器上发生事件时从服务器接收更新.因此,即使没有客户端明确发送请求,我也需要服务器能够将响应发送到特定的客户端浏览器.

I am trying to implement a "Server Push" (or "Reverse Ajax") kind of webpage that will receive updates from the server as and when events occur on the server. So even without the client explicitly sending a request, I need the server to be able to send responses to the specific client browser.

我知道,Comet是这类技术的统称.协议以"Bayeux"为准.但是,当我查看servlet规范时,甚至异步servlet"似乎也可以完成相同的工作.我的意思是我可以使用

I understand that Comet is the umbrella term for these kind of technologies; with 'Bayeux' being the protocol. But when I looked through the servlet spec, even the 'Async servlet' seems to accomplish the same thing. I mean I can define a simple servlet with the

<async-supported>

在web.xml中将

属性设置为true;并且该Servlet将能够异步将响应发送到客户端.然后,我可以拥有一个基于jQuery或ExtJS的ajax客户端,而该客户端只会继续执行

attribute set to true in the web.xml; and that servlet will be able to asynchronously send responses to the client. I can then have a jQuery or ExtJS based ajax client that just keeps doing a

long_polling() 

调用servlet.类似于以下链接中所述的内容 http://www.ibm.com/developerworks/web/library/wa-reverseajax1/index.html#long

call into the servlet. Something like what is described in the link below http://www.ibm.com/developerworks/web/library/wa-reverseajax1/index.html#long

所以我的问题是这个

简单的异步servlet和Comet/Bayeux协议有什么区别?

谢谢

推荐答案

确实,"Comet"是这些技术的术语,但是Bayeux协议仅由少数实现使用.彗星技术可以使用它想要的任何协议.巴约就是其中之一.

It is true that "Comet" is the term for these technologies, but the Bayeux protocol is used only by few implementations. A Comet technique can use any protocol it wants; Bayeux is one of them.

话虽如此,异步servlet解决方案和Comet + Bayeux解决方案之间有两个主要区别.

Having said that, there are two main differences between an async servlet solution and a Comet+Bayeux solution.

第一个区别是Comet + Bayeux解决方案独立于传输Bayeux的协议. 在 CometD 项目中,存在可以承载Bayeux的客户端和服务器的可插拔传输. 您可以使用HTTP来承载它,其中Bayeux是POST请求的内容,但是您也可以使用WebSocket来承载它,而Bayeux是WebSocket消息的有效负载. 如果您使用异步servlet,则无法利用WebSocket,即.

The first difference is that the Comet+Bayeux solution is independent of the protocol that transports Bayeux. In the CometD project, there are pluggable transports for both clients and servers that can carry Bayeux. You can carry it using HTTP, with Bayeux being the content of a POST request, but you can also carry it using WebSocket, with Bayeux being the payload of the WebSocket message. If you use async servlets, you cannot leverage WebSocket, which is way more efficient than HTTP.

第二个区别是异步servlet仅承载HTTP,而处理远程Comet客户端则需要更多.

The second difference is that async servlets only carry HTTP, and you need more than that to handle remote Comet clients.

例如,您可能想要唯一地标识客户端,以便同一页面的2个选项卡会导致2个不同的客户端.为此,您需要在异步Servlet请求中添加一个属性",我们将其称为sessionId.

For example, you may want to identify uniquely the clients, so that 2 tabs for the same page result in 2 different clients. To do this, you need add a "property" to the async servlet request, let's call it sessionId.

接下来,您希望能够对客户端进行身份验证;只有经过身份验证的客户端才能获得sessionId.但是,要区分第一个要进行身份验证的请求和其他已通过身份验证的后续请求,您需要另一个属性,例如messageType.

Next, you want to be able to authenticate a client; only authenticated clients can get a sessionId. But to differentiate between first requests to authenticate and others subsequent requests already authenticated, you need another property, say messageType.

接下来,您希望能够迅速通知由于网络丢失或其他连接问题而导致的断开连接;因此,您需要提出一种心跳解决方案,这样,如果心跳跳动,您就知道连接处于活动状态;如果心跳跳动不了,则表明连接已死,然后执行救援操作.

Next, you want to be able to notify quickly disconnections due to network loss or other connectivity problems; so you need to come up with a heart-beat solution so that if the heart beats you know the connection is alive, if it does not beat you know it's dead, and perform rescue actions.

接下来,您需要断开连接功能.依此类推.

Next you need disconnect features. And so on.

很快,您意识到您正在基于HTTP构建另一个协议.

Quickly you realize that you're building another protocol on top of HTTP.

到那时,最好重用现有的协议(如Bayeux)和成熟的解决方案(如CometD(基于使用Bayeux协议的Comet技术)),从而为您提供:

At that point, it's better to reuse an existing protocol like Bayeux, and proven solutions like CometD (which is based on Comet techniques using the Bayeux protocol) that gives you:

  • Java and JavaScript client libraries with simple yet powerful APIs
  • Java server library to perform your application logic without the need to handle low level details such as HTTP or WebSocket via annotated services
  • Transport pluggability, both client and server
  • Bayeux protocol extensibility
  • Lazy messages
  • Clustering
  • Top performance
  • Future proof: users of CometD before the advent of WebSocket did not change a line of code to take advantage of WebSocket - all the magic was implemented in the libraries
  • Based on standards
  • Designed and maintained by web protocols experts
  • Extended documentation
  • I can continue, but you get the point :)

您不想使用仅将您绑定到HTTP的低级解决方案.您想使用更高级别的解决方案,使您的应用程序从使用的Comet技术和传输Bayeux的协议中抽象出来,以便您的应用程序可以编写一次并利用将来的技术改进.作为技术改进的一个示例,CometD在异步servlet出现之前一直运行良好,现在异步servlet变得更具可伸缩性,因此您的应用程序无需更改应用程序中的任何一行.

You don't want to use a low-level solution that ties you to HTTP only. You want to use a higher level solution that abstracts your application from the Comet technique used and from the protocol that transports Bayeux, so that your application can be written once and leverage future technology improvements. As an example of technology improvement, CometD was working well way before async servlets came into picture, and now with async servlet just became more scalable, and so your application, without the need to change a single line in the application.

通过使用更高级别的解决方案,您可以专注于您的应用程序,而不是专注于如何正确编写异步servlet的繁琐细节(这并不像人们想象的那么容易).

By using a higher level solution you can concentrate on your application rather than on the gory details of how to write correctly an async servlet (and it's not that easy as one may think).

您的问题的答案可能是:您使用Comet + Bayeux是因为您想站在巨人.

The answer to your question could be: you use Comet+Bayeux because you want to stand on the shoulder of giants.

这篇关于异步servlet长轮询和贝叶协议(Comet)之间的区别的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆