Web页面实时刷新技术探讨.docx

上传人:b****8 文档编号:27575119 上传时间:2023-07-03 格式:DOCX 页数:18 大小:22.96KB
下载 相关 举报
Web页面实时刷新技术探讨.docx_第1页
第1页 / 共18页
Web页面实时刷新技术探讨.docx_第2页
第2页 / 共18页
Web页面实时刷新技术探讨.docx_第3页
第3页 / 共18页
Web页面实时刷新技术探讨.docx_第4页
第4页 / 共18页
Web页面实时刷新技术探讨.docx_第5页
第5页 / 共18页
点击查看更多>>
下载资源
资源描述

Web页面实时刷新技术探讨.docx

《Web页面实时刷新技术探讨.docx》由会员分享,可在线阅读,更多相关《Web页面实时刷新技术探讨.docx(18页珍藏版)》请在冰豆网上搜索。

Web页面实时刷新技术探讨.docx

Web页面实时刷新技术探讨

Web页面实时刷新技术探讨

一、总述

随着网络技术的飞速发展,使用B/S结构来实现项目应用已经越来越多,而实时监控一直都是多数行业软件所必备的功能,由此使用Web页面来实现实时监控成了一种必然的需求。

 

二、实时刷新技术

1、传统的页面刷新方式

传统的页面刷新方式很多,常见的有页面间隔一定的时间自动刷新、ActiveX控件、Applet等。

采用页面间隔一定的时间自动刷新的方式,是在网页的头部加入一下代码:

这里是经过20秒跳转到一个新页面,可以将“newPage”设置为本页面即为刷新本页面,刷新间隔时间可以修改“20”为任意时间。

通过这种方式如果并发和访问量较大,服务器就有可能承受不了这种压力,从而造成服务器死机。

使用ActiveX控件的方式需要每个客户端下载安装ActiveX控件,并且客户端浏览器只能使用Windows的IE浏览器。

同样使用Applet需要客户端安装Java运行时。

这些传统的页面刷新方式都或多或少的存在着一些确定,在Web项目应用中的使用也越来越少。

 

2、Ajax轮询

Ajax轮询方式是使用客户端脚本,通过XMLHttpRequest来定时发送请求,从而查询页面数据的更新情况。

通过这种方式,程序实现方便简捷,但客户端频繁的发送请求会给服务器带来很大的压力和客户端处理器负载,如果服务器端没有更新时,这种轮询访问服务器便是无意义的,并且耗费了网络资源与CPU处理资源。

实例说明:

服务器端通过手动控制按钮产生一张图片,客户端显示最新图片及图片的信息内容。

服务器端通过一个按钮btnGet产生图片,按钮事件代码如下所示。

代码清单1:

protectedvoidbtnGet_Click(objectsender,EventArgse)

{

//通过改写一张父图片上的文字来产生新图片

System.Drawing.Imageimage=System.Drawing.Image.FromFile(HttpContext.Current.Server.MapPath("parent.jpg"));

stringcurrTime=System.DateTime.Now.ToString("yyMMddHHmmssffffff");

 

Graphicsg=Graphics.FromImage(image);

g.DrawImage(image,0,0,image.Width,image.Height);

g.DrawString(currTime,newFont("Arial",28),newSolidBrush(Color.Red),10,10);

g.Dispose();

 

stringsavePath="Pic/"+currTime+".jpg";

image.Save(HttpContext.Current.Server.MapPath(savePath));

 

//将最新图片文件名写入到XML文件中

XmlDocumentxmlDoc=newXmlDocument();

xmlDoc.Load(HttpContext.Current.Server.MapPath("newPic.xml"));

XmlNodeListnodeList=xmlDoc.SelectSingleNode("Items").ChildNodes;

 

XmlElementelement=(XmlElement)nodeList[0];

element.SetAttribute("code",currTime);

xmlDoc.Save(HttpContext.Current.Server.MapPath("newPic.xml"));

}

显示图片页面通过两个页面分别显示图片信息与图片内容,显示图片页面内容如下所示。

代码清单2:

//www.w3.org/1999/xhtml">

varxmlHttp;

functionCreateXMLHttp(){

if(window.XMLHttpRequest){

xmlHttp=newXMLHttpRequest();

}

elseif(window.ActiveXObject){

try{xmlHttp=newActiveXObject("Msxml2.XMLHTTP");}

catch(e){

try{xmlHttp=newActiveXObject("Microsoft.XMLHTTP");}

catch(e){}

}

}

xmlHttp=newActiveXObject("Msxml2.XMLHTTP.5.0");

}

 

functionstartXMLHttp(){

CreateXMLHttp();

xmlHttp.onreadystatechange=retDeal;

xmlHttp.open("post","imgInfo.aspx",true);

xmlHttp.setRequestHeader("Content-Type","application/x-www-form-urlencodedcharset=gb2312");

xmlHttp.send();

}

functionretDeal(){

if(xmlHttp.readystate==4){

if(xmlHttp.status==200){

hid1.value=xmlHttp.responseText;

if(hid1.value!

=hid2.value){

hid2.value=hid1.value;

ifrImg.location.reload();

document.getElementById("Content").innerHTML=hid1.value;

}

}

setTimeout(startXMLHttp,2000);

}

}

startXMLHttp()'>

 

3、DWR服务器Push

DWR的反转AJAX功能允许我们从服务器端来控制客服端,而不需要客户端的请求,服务器可以自动把消息发给指定的客户端。

DWR的Push技术是让服务器每次发送广播时,把这个广播推送给客户端,而不用客户端去刷新,DWR的推送是基于长连接的,性能优越。

以服务器端通过手动控制按钮产生一张图片,客户端显示最新图片及图片的信息内容作为实例加以说明。

服务器端通过一个按钮产生图片,页面代码如下所示。

代码清单3:

<%@pagelanguage="java"pageEncoding="UTF-8"%>

DOCTYPEHTMLPUBLIC"-//W3C//DTDHTML4.01Transitional//EN">

Date.prototype.format=function(format){

varo={

"M+":

this.getMonth()+1,//month

"d+":

this.getDate(),//day

"h+":

this.getHours(),//hour

"m+":

this.getMinutes(),//minute

"s+":

this.getSeconds(),//second

"q+":

Math.floor((this.getMonth()+3)/3),//quarter

"S":

this.getMilliseconds()//millisecond

}

if(/(y+)/.test(format))format=format.replace(RegExp.$1,

(this.getFullYear()+"").substr(4-RegExp.$1.length));

for(varkino)if(newRegExp("("+k+")").test(format))

format=format.replace(RegExp.$1,

RegExp.$1.length==1?

o[k]:

("00"+o[k]).substr((""+o[k]).length));

returnformat;

}

 

functiongetNewPic(){

varcurrTime=newDate().format("yyMMddhhmmssS");

varcurrPath="D:

/Program/Java/JavaSpace/ajaxTest/WebContent/";

getPic.createStringMark(currPath+"parent.jpg",currTime,currPath+"Pic/"+currTime+".jpg");

getPic.getNewPicId(currTime);

}

显示图片页面通过两个页面分别显示图片信息与图片内容,显示图片页面内容如下所示。

代码清单4:

<%@pagelanguage="java"pageEncoding="UTF-8"%>

useBeanid="aGetNewPic"scope="page"class="com.getNewPic"/>

DOCTYPEHTMLPUBLIC"-//W3C//DTDHTML4.01Transitional//EN">

functioninit(){

dwr.engine.setActiveReverseAjax(true);//激活反转

}

window.onload=init;//页面初始化方法

functionrefreshImg(){

ifrImg.location.reload();

}

left">最新图片:

<%=aGetNewPic.currPicId%>

id=<%=aGetNewPic.currPicId%>"width="800"height="500">

另外,getNewPic类用于产生新图片、Push处理。

代码清单5:

publicclassgetNewPic{

publicstaticStringcurrPicId="100413101427820";

publicStringgetNewPicId(StringpicId,HttpServletRequestrequest){

if(currPicId==picId)returncurrPicId;

if(picId!

=null)currPicId=picId;

//获得DWR上下文

ServletContextsc=request.getSession().getServletContext();

ServerContextsctx=ServerContextFactory.get(sc);

//获得当前浏览client.jsp页面的所有脚本session

Collectionsessions=sctx.getScriptSessionsByPage("/ajaxTest/client.jsp");

Utilutil=newUtil(sessions);

//处理这些页面中的一些元素

util.setValue("divNewPicId",currPicId);

util.addFunctionCall("refreshImg",null);

returncurrPicId;

}

publicbooleancreateStringMark(StringfilePath,StringmarkContent,StringsavePath)

{

ImageIconimgIcon=newImageIcon(filePath);

ImagetheImg=imgIcon.getImage();

intwidth=theImg.getWidth(null);

intheight=theImg.getHeight(null);

//System.out.println(theImg);

BufferedImagebimage=newBufferedImage(width,height,BufferedImage.TYPE_INT_RGB);

Graphics2Dg=bimage.createGraphics();

g.setColor(Color.red);

g.setBackground(Color.white);

g.drawImage(theImg,0,0,null);

g.setFont(newFont("Arial",Font.PLAIN,28));//字体、字型、字号

g.drawString(markContent,10,10);//画文字

g.dispose();

try

{

FileOutputStreamout=newFileOutputStream(savePath);//输出文件名

JPEGImageEncoderencoder=JPEGCodec.createJPEGEncoder(out);

JPEGEncodeParamparam=encoder.getDefaultJPEGEncodeParam(bimage);

param.setQuality(1,true);

encoder.encode(bimage,param);

out.close();

}

catch(Exceptione)

{returnfalse;}

returntrue;

}

 

}

 

4、与服务端建立长连接

与服务器建立长连接,也就是在显示数据页面中嵌入一个隐藏页面,该隐藏页面主要完成取服务器端所要显示的数据,并且将该页面显示数据的方法写成一个死循环,以此来保持与服务器端的长连接。

同样以服务器端通过手动控制按钮产生一张图片,客户端显示最新图片及图片的信息内容作为实例加以说明。

服务器端通过一个按钮btnGet产生图片,按钮事件代码同代码清单1。

显示图片页面通过两个页面分别显示图片信息与图片内容,显示图片页面内容如下所示。

代码清单6:

//www.w3.org/1999/xhtml">

functionwritePicInfo(str){

if(window.document.getElementById("divNewPicId").innerText!

=str){

window.document.getElementById("divNewPicId").innerText=str;

ifrImg.location.reload();

}

}

functiononload(){

varifrpush=newActiveXObject("htmlfile");//创建对象

ifrpush.open();

varifrDiv=ifrpush.createElement("div");//添加一个DIV

ifrpush.appendChild(ifrDiv);//添加到htmlfile

ifrpush.parentWindow.writePicInfo=writePicInfo;//注册javascript方法

ifrDiv.innerHTML="";//在div里添加iframe

ifrpush.close();

}

onload();

left">最新图片:

其中,隐藏页面getNew.aspx代码如下所示。

代码清单7:

protectedoverridevoidRender(HtmlTextWriteroutput)

{

stringstr;

while(true)//死循环保持长链接

{

//读取最新图片信息

XmlDocumentxmlDoc=newXmlDocument();

xmlDoc.Load(HttpContext.Current.Server.MapPath("newPic.xml"));

XmlNodeListnodeList=xmlDoc.SelectSingleNode("Items").ChildNodes;

XmlElementelement=(XmlElement)nodeList[0];

stringnewPicId=element.GetAttribute("code");

 

str="";

this.Context.Response.Write(str);

this.Context.Response.Flush();

System.Threading.Thread.Sleep(2000);

}

}

代码中的“htmlfile”是一个类似JavaScript中Window对象的一个ActiveXObject,它内部也是DOM结构,将作为隐藏帧的IFrame写入这个对象中,这样可以解决进度条一直为读取状态的问题。

 

5、RTMP协议传输

随着网络技术的迅猛发展,视频、音频等多媒体通信需求越来越多,Adobe公司开放了RTMP(theReal-timeMessagingProtocol)协议规范,RTMP协议作为客户端和服务器端的传输协议,这是一个专门为高效传输视频、音频和数据而设计的TCP/IP协议。

其优秀产品Flex是用于构建和维护在所有主要浏览器、桌面和操作系统一致地部署的极具表现力的Web应用程序的高效率的开放源码框架。

从目前的应用来说,RTMP主要用于音、视频的传输,流视频服务器就是FMS(Flash Media Server),其原称为FCS(Flash Communication Server),技术范畴能应用到诸如Flash聊天室、视频会议等领域。

以一个实现聊天功能的Flex程序为例,显示聊天内容代码如下所示。

代码清单8:

xmlversion="1.0"encoding="utf-8"

展开阅读全文
相关资源
猜你喜欢
相关搜索

当前位置:首页 > 表格模板 > 表格类模板

copyright@ 2008-2022 冰豆网网站版权所有

经营许可证编号:鄂ICP备2022015515号-1