设为首页 收藏本站
查看: 1008|回复: 0

[经验分享] !锄作田心有明镜,能辨是非,纵有明末遗恨,也不过沧海一声笑。

[复制链接]

尚未签到

发表于 2017-2-28 08:29:51 | 显示全部楼层 |阅读模式
CometD 框架
  CometD 框架是基于 HTTP 的事件驱动通信解决方案。CometD 框架提供了一个 Java 服务器部件和一个 Java 客户端部件,还有一个基于 jQuery 和 Dojo 的 JavaScript 客户端库。CometD 使用了一个叫 Bayeux 的标准化通信协议,充许您针对于消息确认、流控制、同步、集群等方面进行扩展。
  
CometD 的事件驱动方法非常适合事件驱动 Web 开发的新概念。正如传统的桌面用户界面那样,所有的组件均通过总线进行通信,以便发送通知和接收事件。因此所有的通信都是异步的。
  
CometD 框架:


  • 提供了一个称为 Oort 的集群模块,该模块使您能够运行多个CometD Web 服务器,将它们看作是负载均衡器背后的集群中的节点,从而扩展许多 HTTP 连接。
  • 支持安全策略,允许更具体地配置由谁通过哪条通道发送消息。- 更好地与 Spring 和 Google Guice(依赖注入框架)集成。
Bayeux 协议
  它定义的消息通过命名通道进行路由并且能够进行交互传 送:server -> client, client -> server 甚至 client -> client (当然还是需要通过server中转)。Bayeux 协议主要基于 HTTP 来传输低延迟的、异步的事件消息。采用Publish/Subscribe的模式,允许服务端异步push消息到客户端。CommetD实现可以基于流 (streaming) 和长轮询 (long polling)。

服务器和内部构件
  CometD 与三个传输协议绑定在一起:JSON、JSONP 和 WebSocket。他们都依赖于 Jetty Continuations 和 Jetty WebSocket API。在默认情况下,可以在 Jetty 6、Jetty 7、和 Jetty 8 中以及其他所有支持 Servlet 3.0 Specification 的服务中使用 CometD。可以使用与扩展相同的方式添加和开发传输协议。您应该能够编写支持 Grizzly WebSocket API 和其他 API 的传输协议,然后再在配置 CometD 服务器的步骤中添加这些协议。 Cometd是一个提供多种开发语言的Bayeux项目,由Dojo基金会提供支持。

Comet 的反向 Ajax
  客户端向服务端发送请求,当请求到达的时候服务端可以立即将处理结果发送给客户端,也可以累计客户端发送的请求再连续的发送给客户端。
  
DSC0000.gif

CometD 的构架视图
DSC0001.gif


Subscribe
  订阅是将订阅某个Channel的客户端所对应的ContinuationClient对象加入到对应的channel的subscribers列表中。订阅成功后进行客户端的长轮训,服务端会将当前的请求封装到一个Continuation中,并将Continuation对象设到ContinuationClient对象中,然后将ContinuationClient对象挂起。

Publish
  服务端接受到信息的时候,会找到订阅的channel,然后进行数据的广播。这个时候调用channel的push 方法,对每一个订阅当前channel客户进行分发。将ContinuationClient中的Continuation唤醒resume。
  客户端与服务端建立起连接
  
send信息到服务端的时候会带上clientId,本身还带上id且id是递增
  
DSC0002.png
  
客户端通过channel传递test1,服务端同时将信息返回
  
DSC0003.png
  
服务器停止,关闭服务的时候客户端检测信息
  
DSC0004.png
  前端画面建立连接
(function($)  
{
  var cometd = $.cometd;
  $(document).ready(function()
  {
  /**

  * Therefore the code that you put in the _connectionEstablished() function must be>  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
  $(&quot;#btnTest&quot;).click(function(){
  cometd.publish('/service/hello', { name: $(&quot;#txtValue&quot;).val()});
  })
  });
  }
  }
  

  // Disconnect when the page unloads
  $(window).unload(function()
  {
  cometd.disconnect(true);
  });
  

  var cometURL = location.protocol + &quot;//&quot; + location.host + config.contextPath + &quot;/cometd&quot;;
  cometd.configure({
  url: cometURL,
  logLevel: 'debug',
  });
  //进行握手
  cometd.addListener('/meta/handshake', _metaHandshake);
  //建立连接
  cometd.addListener('/meta/connect', _metaConnect);      
  cometd.handshake();
  });
  
})(jQuery);

其它消息推送技术


  • ActiveMQ AJAX - publish and subscribe to JMS events directly from javascript in the browser. This is quite basic, with less bells and whistles than the other approaches, but because of it's simplicity, might be a good base to start with if you a) already use activemq, b) like not having too many layers of abstraction
  • Atmosphere - Event based framework, can auto detected the best communication mechanism based on which webserver and which broswer are currently in use. A pretty nice framework, which supports a full spread of browsers and web severs, even down to IE6. And there are examples of using Atmosphere with spring MVC and Spring Integration.
  • Cometd - An implementation of the Bayeux protocol (to auto-negotiate the best connection type) based on jetty/hightide. Jetty was the first java webserver to support continuations, now part of the latest Servlet spec. Cometd take Jetty and wraps it up with JS client libraries for autodetection of the best connection mechanism to the browser.
  • Vert.x - An event based server platform that you can build on top of. There has been some controversy around Vert.x recently when it's author left VMware, but VMware retained the project. It now looks like version 2 will be>
  • HTML5 EventSource - Standards based way of sending events to the browser. No mechanism for sending events back to the server. It's interesting, but given you need to implment a fallback for IE6, IE7 and IE8, it might not be your best choice, for now.
文献资料
  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
  

运维网声明 1、欢迎大家加入本站运维交流群:群②:261659950 群⑤:202807635 群⑦870801961 群⑧679858003
2、本站所有主题由该帖子作者发表,该帖子作者与运维网享有帖子相关版权
3、所有作品的著作权均归原作者享有,请您和我们一样尊重他人的著作权等合法权益。如果您对作品感到满意,请购买正版
4、禁止制作、复制、发布和传播具有反动、淫秽、色情、暴力、凶杀等内容的信息,一经发现立即删除。若您因此触犯法律,一切后果自负,我们对此不承担任何责任
5、所有资源均系网友上传或者通过网络收集,我们仅提供一个展示、介绍、观摩学习的平台,我们不对其内容的准确性、可靠性、正当性、安全性、合法性等负责,亦不承担任何法律责任
6、所有作品仅供您个人学习、研究或欣赏,不得用于商业或者其他用途,否则,一切后果均由您自己承担,我们对此不承担任何法律责任
7、如涉及侵犯版权等问题,请您及时通知我们,我们将立即采取措施予以解决
8、联系人Email:admin@iyunv.com 网址:www.yunweiku.com

所有资源均系网友上传或者通过网络收集,我们仅提供一个展示、介绍、观摩学习的平台,我们不对其承担任何法律责任,如涉及侵犯版权等问题,请您及时通知我们,我们将立即处理,联系人Email:kefu@iyunv.com,QQ:1061981298 本贴地址:https://www.yunweiku.com/thread-348160-1-1.html 上篇帖子: More about dubbo 下篇帖子: 分布式架构--第一篇--项目拆分(maven命令生成多模块项目)
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

扫码加入运维网微信交流群X

扫码加入运维网微信交流群

扫描二维码加入运维网微信交流群,最新一手资源尽在官方微信交流群!快快加入我们吧...

扫描微信二维码查看详情

客服E-mail:kefu@iyunv.com 客服QQ:1061981298


QQ群⑦:运维网交流群⑦ QQ群⑧:运维网交流群⑧ k8s群:运维网kubernetes交流群


提醒:禁止发布任何违反国家法律、法规的言论与图片等内容;本站内容均来自个人观点与网络等信息,非本站认同之观点.


本站大部分资源是网友从网上搜集分享而来,其版权均归原作者及其网站所有,我们尊重他人的合法权益,如有内容侵犯您的合法权益,请及时与我们联系进行核实删除!



合作伙伴: 青云cloud

快速回复 返回顶部 返回列表