HTML5_Communication API介绍

1. Communcation API 介绍

  通信(Communication)API是HTML5中用来实现正在运行的两个页面之间进行通信和信息共享的API,在开发Web应用程序时,其主要功能就是可以实现应用程序进程间通信。目前浏览器对其支持情况如下图:

  

 

  2. Communcation API 运行流程

  通信(Communication)API是HTML5中用来实现正在运行的两个页面之间进行通信和信息共享的API,

  

 

  如上图所示,

  Application在进行通信的时候被分为Host page(发送信息的页面),Client Page(接受信息的页面)。

  Host page在发送信息的时候可以在代码中指定其信息为广播还是指定目标和端口进行发送。

  发送之后,相应的接收端可以接收相同协议的Host page发送过来的消息。

  当然,接收端有一个权限验证的过程,验证其消息来源于可信区域。

  3. Communication API使用介绍

  通信(Communication)API在HTML5中的使用的使用步骤主要分为三步,如下图所示:

  

 

  从图中,大家可以看出如何写相应的代码。

  在Host page端onSendMessage函数负责发送信息,并指定信息的发送方式:

function onSendMessage(e) {
   //Loop through all the open windows and send the message
    for( var d = 0; d <  windows.length; d++){
      windows[d].postMessage($(
'#message' ).val(),  'http://localhost' );
      windows[d].focus();
    }
    //Reset the textarea
    $(
'#message' ).val( '' );
  }

  在Client page端messageHandler函数来接收信息。

function messageHandler(e) {

   //Check the origin of the message
  switch(e.origin) {
     //if it is the domain that we are listening from
    case  "http://experiments.72lions.com":
      $( '#message').append( '<div>' + e.data +  '</div>').css( 'display', 'block').stop().animate({opacity:1}, 200);
    break;
  default:
     //Do nothing if the domain is not the one we are listening from
  }
}

  运行结果如图所示。

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值