本文主要探讨用于构建实时跨源通信的两个模块:跨文档消息通信(cross document messaging)和xmlhttprequestlevel2。通过这两个模块,我们可以构建不同域间进行安全通信的web应用。
一、跨文档消息通信 出于安全方面的看的考虑,运行在同一浏览器中的框架、标签页、窗口间的通信一直多受到严格的限制。但是现实中还存在一些合理的让不同站点的内容能在浏览器内进行交互的需求,其中mashup就是一个典型的例子,它是各种不同应用的结合体。为了满足上述需求,引入了一种新的功能:跨文档消息通信。其可以确保iframe、标签页、窗口间安全地进行跨源通信。
发送消息使用postmessage api,其示例代码如下:
chatframe.contentwindow.postmessage(content,url);
接受消息时,需要在页面中添加一个事件处理函数,当消息到达时,通过检查消息的来源来决定如何对这条消息如何处理,示例代码如下:
window.addeventlistener("message",messagehandler,true);
function messagehandler(e){
switch(e.origin){//表示数据发送源
case "friend.example.com":
//处理消息
processmessage(e.data);//发送方实际传送的消息
break;
default:
//其他消息来源
//消息被忽略。
}
}
postmessage api提供了一种交互方式,使得不同源的iframe之间可以进行消息通信。
html5 通过引入源的感念对域安全进行了阐明和改进。源是网络上用来建立信任关系的地址的子集。源由规则(scheme)、主机(host)、端口(port)组成,例如由于scheme(https、http)不同,则源不同。
跨源通信通过 源来确定发送者,这就使得接收方可以忽略或者拒绝来自不可信源的消息。同时需要通过添加监听事件来接受消息,以避免被不可信应用程序的信息所干扰。但是在使用外来消息时,即便是可靠的数据源,也同样要谨慎,以防止内容注入。
在使用postmessage api时,需要遵循以下步骤:
1、检查浏览器是否支持
if(typeof window.postmessage === undefined){
//浏览器不支持postmessage
}
2、发送消息
window.postmessage("hello","xx.example.com");
第一个参数包含要发送的数据,第二个参数时消息传递的目的地。
如果要发送消息给iframe,则使用如下代码:
document.getelementbyid("iframe")[0].contentwindow.postmessage("hello","xx.example.com");
3、监听消息事件
window.addeventlistener("message",messagehandler,true);
var originwhitelist = ["a.example.com","b.example.com","c.example.com"];
function messagehandler(e){
if(checkwhitelist(e.origin)){
processmessage(e.data);//发送方实际传送的消息
}else{
//忽略发送的消息
}
}
function checkwhitelist(origin){
for(var i = 0; i<originwhitelist.length; i++){
if(origin === originwhitelist[i]){
return true;
}
}
return false;
}
二、xmlhttprequestlevel2 xmlhttprequestlevel2是xmlhttprequest的改进版本,主要涉及:跨源xmlhttprequess和进度事件(progress events)。
xmlhttprequest仅限于同源通信,xmlhttprequestlevel2通过跨资源共享实现(cross origin resource sharing)跨源xmlhttprequests。
在xmlhttprequest中通过readystatechange事件来响应进度,但是其在某些浏览器中不被兼容。xmlhttprequestlevel2用了一个有意义的名字progress进度来命名进度事件。其进度事件的名称主要有loadstart、progress、abort、error、load、loadend。通过对程序属性设置回调函数,可以实现对这些事件的监听。
在使用xmlhttprequestlevel2时,需要遵循以下步骤:
1、检查浏览器是否支持
var xhr = new xmlhttprequest();
if(typeof xhr.withxredentials === undefined){
//浏览器不支持xmlhttprequest
}
2、构建跨源请求
var crossoriginrequest = new xmlhttprequest();
crossoriginrequest.open("get","http://www.example.com",true);
在请求过程中,务必确保能够监听到错误,以找出出错原因,解决问题。
3、使用进度事件
crossoriginrequest.onprogress = function(e){
var total = e.total;
var loaded = e.loaded;
if(e.lengthcomputable){
//处理其他事情
}
}
crossoriginrequest.upload..onprogress = function(e){
var total = e.total;
var loaded = e.loaded;
if(e.lengthcomputable){
//处理其他事情
}
}
三、postmessage api示例应用以跨源聊天应用为例,来演示门户页面和聊天部件之间的交互。
1、创建postmessageportal.html页面
<!doctype html>
<head>
<meta http-equiv="content-type" content="text/html; charset=utf-8">
<title>跨源通信-webchat</title>
<link rel="stylesheet" href="styles.css">
</head>
<h1>跨源通信门户</h1>
<p><b>源</b>: http://portal.example.com:8080</p>
状态 <input type="text" id="statustext" value="online">
<button id="sendbutton">更改状态</button>
<p>
使用postmessage发送一个状态,以更新包含在此页面中的widgetiframe。
</p>
<iframe id="widget" src="http://chat.example.net:8080/communication/postmessagewidget.html"></iframe>
<script>
var targetorigin = "http://chat.example.net:8080";
var notificationtimer = null;
function messagehandler(e){
if(e.origin == targetorigin){
notify(e.data);
}else{
//忽略
}
}
function sendstring(s){
document.getelementbyid("widget").contentwindow.postmessage(s,targetorigin);
}
function notify(message){
alert(message);
}
function sendstatus(){
var statustext = document.getelementbyid("statustext").value;
sendstring(statustext);
}
function loaddemo(){
document.getelementbyid("sendbutton").addeventlistener("click",sendstatus,true);
sendstatus();
}
window.addeventlistener("load",loaddemo,true);
window.addeventlistener("message",messagehandler,true);
</script>
2、创建postmessagewidget.html
<!doctype html>
<head>
<meta http-equiv="content-type" content="text/html; charset=utf-8">
<title>widget</title>
<link rel="stylesheet" href="styles.css">
</head>
<h1>widget iframe</h1>
<p><b>源</b>: http://chat.example.net:8080</p>
<p>通过门户设置状态为: <strong id="status"></strong> <p>
<p>
<input type="text" id="messagetext" value="widget notification.">
<button id="actionbutton">发送通知</button>
</p>
<script>
var targetorigin = "http://portal.example.com:8080";
window.addeventlistener("load",loaddemo,true);
window.addeventlistener("message",messagehandler,true);
function loaddemo(){
document.getelementbyid("actionbutton").addeventlistener("click",
function() {
var messagetext = document.getelementbyid("messagetext").value;
sendstring(messagetext);
}, true);
}
function messagehandler(e) {
if (e.origin === "http://portal.example.com:8080") {
document.getelementbyid("status").textcontent = e.data;
} else {
// ignore messages from other origins
}
}
function sendstring(s) {
window.top.postmessage(s, targetorigin);
}
</script>
注意:第一、上述的两个页面需要部署到web服务器;第二、两个页面必须来自不同的域。如果要在本机部署,则需要更改hosts文件,增加:
127.0.0.1 portal.example.com
127.0.0.1 chat.example.net
修改完成后,需要关闭浏览器,再次重新打开。
四、xmlhttprequestlevel2示例应用1、创建crossoriginupload.html页面:
<!doctype html>
<head>
header(“access-control-allow-origin: *”);
<meta http-equiv="content-type" content="text/html; charset=utf-8">
<title>上传地理数据</title>
<link rel="stylesheet" href="styles.css">
</head>
<script>
function loaddemo() {
var dataelement = document.getelementbyid("geodata");
dataelement.textcontent = json.stringify(geodata).replace(",", ", ", "g");
var xhr = new xmlhttprequest()
if (typeof xhr.withcredentials === undefined) {
document.getelementbyid("support").innerhtml = "浏览器不支持跨源xmlhttprequest";
} else {
document.getelementbyid("support").innerhtml = "浏览器支持跨源xmlhttprequest";
}
var targetlocation = "http://geodata.example.net:8080/upload";
function setprogress(s) {
document.getelementbyid("progress").innerhtml = s;
}
document.getelementbyid("sendbutton").addeventlistener("click",
function() {
xhr.upload.onprogress = function(e) {
var ratio = e.loaded / e.total;
setprogress("已上传" + ratio + "%");
}
xhr.onprogress = function(e) {
var ratio = e.loaded / e.total;
setprogress("已下载" + ratio + "%");
}
xhr.onload = function(e) {
setprogress("完成");
}
xhr.onerror = function(e) {
setprogress("错误");
}
xhr.open("post", targetlocation, true);
geodatastring = dataelement.textcontent;
xhr.send(geodatastring);
}, true);
}
window.addeventlistener("load", loaddemo, true);
</script>
<h1>xmlhttprequest level 2</h1>
<p id="support"></p>
<h4>上传地理数据:</h4>
<textarea id="geodata">
</textarea>
</p>
<button id="sendbutton">上传</button>
<script>
geodata = [[39.080018000000003,
39.112557000000002,
39.135261,
39.150458,
39.170653000000001,
39.190128000000001,
39.204510999999997,
39.226759000000001,
39.238483000000002,
39.228154000000004,
39.249400000000001,
39.249533,
39.225276999999998,
39.191253000000003,
39.167993000000003,
39.145685999999998,
39.121620999999998,
39.095761000000003,
39.080593,
39.053131999999998,
39.02619,
39.002929000000002,
38.982886000000001,
38.954034999999998,
38.944926000000002,
38.919960000000003,
38.925261999999996,
38.934922999999998,
38.949373000000001,
38.950133999999998,
38.952649000000001,
38.969692000000002,
38.988512999999998,
39.010652,
39.033088999999997,
39.053493000000003,
39.072752999999999],
[-120.15724399999999,
-120.15818299999999,
-120.15600400000001,
-120.14564599999999,
-120.141285,
-120.10889900000001,
-120.09528500000002,
-120.077596,
-120.045428,
-120.0119,
-119.98897100000002,
-119.95124099999998,
-119.93270099999998,
-119.927131,
-119.92685999999999,
-119.92636200000001,
-119.92844600000001,
-119.911036,
-119.942834,
-119.94413000000002,
-119.94555200000001,
-119.95411000000001,
-119.941327,
-119.94605900000001,
-119.97527599999999,
-119.99445,
-120.028998,
-120.066335,
-120.07867300000001,
-120.089985,
-120.112227,
-120.09790700000001,
-120.10881000000001,
-120.116692,
-120.117847,
-120.11727899999998,
-120.14398199999999]
];
</script>
<p>
<b>状态: </b> <span id="progress">准备</span>
</p>
注意:部署web应用,运行crossoriginupload.html页面时,可能会才出现如下的提示错误:
这是因为访问一页面的域与所请求的域非同源造成的。且浏览器是根据响应头的规则来确定这个域是否同源可以接收。
因此我们需要geodata.example.net:8080/upload在返回内容时,设置header access-control-allow-origin,即:
response.addheader("access-control-allow-origin","*") ;
浏览器在接收到服务器返回信息时,会检查响应头的access-control-allow-origin,它的值标识请求内容所允许的域。如果将服务器设置access-control-allow-origin为*,表明该返回信息允许所有源访问。如果设置为具体的域,如xx.com,就表明除了同源外,只允许域来自xx.com的访问。
以上就是html5编程之旅-communication技术初探的内容。