CometD 框架是基于 HTTP 的事件驱动通讯解决方案。CometD 框架提供了一个 Java 服务器部件和一个 Java 客户端部件,还有一个基于 jQuery 和 Dojo 的 JavaScript 客户端库。CometD 使用了一个叫 Bayeux 的标准化通讯协议,充许您针对于消息确认、流控制、同步、集群等方面进行扩展。
CometD 的事件驱动方法很是适合事件驱动 Web 开发的新概念。正如传统的桌面用户界面那样,全部的组件均经过总线进行通讯,以便发送通知和接收事件。所以全部的通讯都是异步的。
CometD 框架:javascript
它定义的消息经过命名通道进行路由而且可以进行交互传 送:server -> client, client -> server 甚至 client -> client (固然仍是须要经过server中转)。Bayeux 协议主要基于 HTTP 来传输低延迟的、异步的事件消息。采用Publish/Subscribe的模式,容许服务端异步push消息到客户端。CommetD实现能够基于流 (streaming) 和长轮询 (long polling)。html
CometD 与三个传输协议绑定在一块儿:JSON、JSONP 和 WebSocket。他们都依赖于 Jetty Continuations 和 Jetty WebSocket API。在默认状况下,能够在 Jetty 六、Jetty 七、和 Jetty 8 中以及其余全部支持 Servlet 3.0 Specification 的服务中使用 CometD。可使用与扩展相同的方式添加和开发传输协议。您应该可以编写支持 Grizzly WebSocket API 和其余 API 的传输协议,而后再在配置 CometD 服务器的步骤中添加这些协议。 Cometd是一个提供多种开发语言的Bayeux项目,由Dojo基金会提供支持。前端
客户端向服务端发送请求,当请求到达的时候服务端能够当即将处理结果发送给客户端,也能够累计客户端发送的请求再连续的发送给客户端。
java
订阅是将订阅某个Channel的客户端所对应的ContinuationClient对象加入到对应的channel的subscribers列表中。订阅成功后进行客户端的长轮训,服务端会将当前的请求封装到一个Continuation中,并将Continuation对象设到ContinuationClient对象中,而后将ContinuationClient对象挂起。web
服务端接受到信息的时候,会找到订阅的channel,而后进行数据的广播。这个时候调用channel的push 方法,对每个订阅当前channel客户进行分发。将ContinuationClient中的Continuation唤醒resume。ajax
客户端与服务端创建起链接
send信息到服务端的时候会带上clientId,自己还带上id且id是递增
客户端经过channel传递test1,服务端同时将信息返回
服务器中止,关闭服务的时候客户端检测信息
spring
前端画面创建链接安全
(function($) { var cometd = $.cometd; $(document).ready(function() { /** * Therefore the code that you put in the _connectionEstablished() function must be idempotent. In other words, make sure that if the _connectionEstablished() function is called more than one time, it will behave exactly as if it is called only once. * @returns */ function _connectionEstablished() { $('#body').append('<div>CometD Connection Established</div>'); } function _connectionBroken() { $('#body').append('<div>CometD Connection Broken</div>'); } function _connectionClosed() { $('#body').append('<div>CometD Connection Closed</div>'); } // Function that manages the connection status with the Bayeux server var _connected = false; // 检测会话链接是否创建 function _metaConnect(message) { if (cometd.isDisconnected()) { _connected = false; _connectionClosed(); return; } var wasConnected = _connected; _connected = message.successful === true; if (!wasConnected && _connected) { _connectionEstablished(); } else if (wasConnected && !_connected) { _connectionBroken(); } } // Function invoked when first contacting the server and // when the server has lost the state of this client var loop=0; function _metaHandshake(handshake) { if (handshake.successful === true) { cometd.batch(function() { //订阅 cometd.subscribe('/hello', function(message) { $('#body').append('<div>Server Says: ' + message.data.greeting + '</div>'); }); // Publish on a service channel since the mge is for the server only $("#btnTest").click(function(){ cometd.publish('/service/hello', { name: $("#txtValue").val()}); }) }); } } // Disconnect when the page unloads $(window).unload(function() { cometd.disconnect(true); }); var cometURL = location.protocol + "//" + location.host + config.contextPath + "/cometd"; cometd.configure({ url: cometURL, logLevel: 'debug', }); //进行握手 cometd.addListener('/meta/handshake', _metaHandshake); //创建链接 cometd.addListener('/meta/connect', _metaConnect); cometd.handshake(); }); })(jQuery);
cometd
wa-reverseajax
An Introduction To WebSockets
What are Long-Polling, Websockets, Server-Sent Events (SSE) and Comet
Servlet 3.0
push notification for java web app
cometd-java-examples服务器