8***@qq.com
8***@qq.com
  • 发布:2016-10-28 12:08
  • 更新:2016-10-28 12:08
  • 阅读:2445

分享另一个(WWW资源)差量升级的方法

分类:HBuilder

HBuider 提供差量升级方法,但需要为每级来做资源包。
分享一个,我一直用的(www资源)差量升级方法:
此方法:是客户端在启动时,先从服务器获取文件列表,然后对比本地文件的修改时间来升级相应的文件。
我用着挺爽的,修改、删除、添加功能,改变页面,客户端只要重新就是最新的。

服务器端(ASP)代码:比如 文件是 files.asp
[HOMEPAGE]c_main.html
<%
show "c_main.html"
show "c_chat.html"
show "c_setup.html"
show "c_showmoney.html"

show "js/enum.js"
show "js/date.js"
show "js/const.js"

show "image/message.png"
show "image/phone.png"
show "image/tablebody.jpg"
show "image/task.png"
show "css/main.css"
show "css/jquery.mobile-1.4.2.min.css"

'==================================FUNCTION========================
sub show(file)
dim md,fso,f
set fso=server.createobject("scripting.filesystemobject")
if fso.FileExists(server.mappath(file)) then
set f=fso.GetFile(server.mappath(file))
md=f.DateLastModified
response.write "/" & file &"|" & md & "|" & f.size & vbcrlf
end if
End Sub
===================END===============================================
上面是服务器端代码,
[HOMEPAGE] 是指定启动页面。
show 函数是显示需要更新的文件的信息,格式是 /filename/修改时间/文件大小[最后是回车]
注:以前的做法是直接遍历所有文件,但有一次不小心做备份,打包成rar,造成了客户端也下载了。所以还是指定可靠些。

下面是HBuilder 端代码

var RootUrl="服务器的地址"; //不用加http
var LocalRoot="_doc";
var ServerPath="/保存路径";
var HomePage;
var Downcount=0;

    document.addEventListener('plusready', function(){  
        //console.log("所有plus api都应该在此事件发生后调用,否则会出现plus is undefined。"  
        DownloadRes();  

    });  

    function StartProgram(){  

        location.href="..\/doc\/"+ServerPath.substr(1) + HomePage;  
    }  

    function Down(name){  
        var down=plus.downloader.createDownload("http://"+RootUrl +ServerPath+ name,{filename:LocalRoot +ServerPath + name},function(down,status) {  
            plus.downloader.enumerate(function (downs) {  
                Downcount--;  
                if (Downcount==0) { //都下载完了执行  
                    StartProgram();  
                }  
            });  
        });  
        down.start();  
    }  

    function DownloadRes(){  
        plus.downloader.clear();  
        var xhr=new plus.net.XMLHttpRequest();  
        xhr.onreadystatechange=function(){  
            if (xhr.readyState==4 && xhr.status==200) {  
                var body=xhr.responseText;  
                lines=body.split("\n");  
                for(var i=0;i<lines.length;i++){  
                    var attr=lines[i].split("|");  

                    if (attr[0]=="") continue;  
                    if (attr[0].indexOf("[HOMEPAGE]")==0) {  
                        HomePage="/"+ attr[0].substr(10);  
                        continue;  
                    } else {  
                        (function(name,time,ii,leng){  
                            plus.io.resolveLocalFileSystemURL(LocalRoot+ServerPath +name,function(fs){  
                                fs.file(function (f) {  
                                    if (f.lastModifiedDate<new Date(time)) {  
                                        Downcount++;  
                                        Down(name);  
                                    } else {  
                                        if (ii==leng-2 && Downcount==0) {  
                                            StartProgram();  
                                        }  
                                    }  

                                });  
                            },function(e) {  
                                Downcount++;  
                                Down(name);  
                            });  
                        })(attr[0],attr[1],i,lines.length);  
                    }  
                }  
            }  
        }  

        xhr.open("GET","http://"+ RootUrl +"服务器files.asp");  
        xhr.send();  
    }  
0 关注 分享

要回复文章请先登录注册