首页 | 新闻 | 新品 | 文库 | 方案 | 视频 | 下载 | 商城 | 开发板 | 数据中心 | 座谈新版 | 培训 | 工具 | 博客 | 论坛 | 百科 | GEC | 活动 | 主题月 | 电子展
返回列表 回复 发帖

反向 Ajax,第 4 部分 Atmosphere 和 CometD(3)

反向 Ajax,第 4 部分 Atmosphere 和 CometD(3)

在客户端,该示例没有进行任何扩展,只是使用原始的 CometD 代码,如 清单 6 中所示:
清单 6. CometD 客户端代码
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
// First create t cometd object and configure it

var cometd = new $.Cometd('CometD chat client');
cometd.configure({
    url: document.location + 'cometd',
    logLevel: 'debug'
});
cometd.websocketEnabled = 'WebSocket' in window;

// Then we register some listeners. Meta channels (those with
// the form /meta/<name> are specific reserved channels)

cometd.addListener('/meta/disconnect', function(message) {
    [...]
});

cometd.addListener('/meta/connect', function(message) {
    [...]
});

// Then, starting a connexion can be done using:

cometd.handshake();

// And subscriptions with:

cometd.subscribe('/chatroom', function(event) {
    [...] //  event.data holds the message
});

// We finally send data to the chatroom like this:

cometd.publish('/chatroom', msg);




CometD 的客户端 API 功能强大、可以扩展并且易于理解和使用。本文只介绍了 Web 应用程序的主要部件,所以请参见 ,以便更好地了解 CometD 的强大。
表 2 概括了使用 CometD 框架的利与弊。
表 2. CometD 的利与弊利弊CometD 提供了一套从客户端到服务器端以及从独立 Java 客户端到服务器的完整解决方案。该框架拥有大量的相关文档、一个出色的 API,并且易于使用。最重要的是,它拥有一个事件驱动方法。CometD 和 Bayeux 是许多事件驱动 Web 应用程序的组成部分。其他的反向 Ajax 框架并不提供任何事件驱动机制,这迫使最终用户必须开发自己的定制解决方案。
CometD 支持许多必要的特性,如重新连接、可靠的超时检测、回退、批处理、消息确认以及更多您在其他反向 Ajax 框架中无法找到的特性。CometD 允许实现最可靠、延迟最短的通信。
CometD 目前除了支持 Jetty for Comet (Tomcat) 之外,并不支持任何 Servlet 2.5 容器,也不支持 Glassfish/Grizzly WebSocket。

结束语Atmosphere 和 CometD 都是可靠的开源反向 Ajax 解决方案。在 Ovea,我们的选择是 CometD,因为我们要为集群环境中的移动设备开发可伸缩的事件驱动 Web 应用程序,并且我们可以完全控制基础架构(我们使用了 Jetty)。然而,如果您要销售 Web 应用程序,并且想让您的反向 Ajax 特性可以在尽可能多的服务器上正常运作,不进行额外开发的话,CometD 未必是最好的选择。但是,现在越来越多的 Web 容器都支持 Servlet Specification 3.0,这极大地减少了 CometD 的限制。说起传输层,主要的区别目前仍然取决于 WebSocket 支持。
返回列表