Meter 上传apk文件后,何时,如何调用服务器的解析方法 与结果处理

原文:http://blog.csdn.net/casun_li/article/details/46301027


我现在的前提条件是已经用CollectionFS上传apk文件成功,并且用aapt能解析apk文件的包包,版本等信息。

本文说的是,上传文件成功后何时调用解析方法,和解析后如何处理。


1,第一种方法,我采用的是;通过Meteor.call()调用服务器的解析方法。

 client:

 FileRe.insert(files[i], function (err, fileObj) {
    if (err) {
        alert('上传文件失败');
        return;
    }

    var url = FileRe.fileUrl(fileObj._id, fileObj.name());
    pushApk.addPushApk(fileObj.name(),'pkg', 'ver', url);

    callGetApkMsg(fileObj);//调用Meteor.call()

});

var callGetApkMsg=function(fileObj){

    var intervalHandle = Meteor.setInterval(function () {

        if (fileObj.hasStored("file_re")) {/等文件存储成功后再调用

            Meteor.call('getApkMsg',fileObj._id,fileObj.name(),function(error,msg){
                if(error){
                    console.log("getApkMsg "+ " error:"+error);
                    return;
                }
                console.log("getApkMsg "+ " msg:"+EJSON.stringify(msg));

            });

            console.log("file has stored, close out interval");
            // file has stored, close out interval
            Meteor.clearInterval(intervalHandle);
        }else{
            console.log("fileObj hasn't stored Inside interval---100--------");
        }
    }, 1000);

}

server:

Meteor.methods({
    getApkMsg:function(_id,name){

        var storepath= "~/meteor_uploads"+"/"+"file_records"+"-"+_id+"-"+name;

        console.log("Meteor.methods storePath   "+storepath);

        var appinfo=parseApkFile(storepath,function(msg){//parseApkFile解析方法

            pushApkRe.updatePushApkMsg(_id,msg['packageName'],msg['versionName'])//对结果的处理    

        });

        return appinfo;//appinfo is undefined . so don't use on client
    }
});

但是这出现一个异常:
Error: Meteor code must always run within a Fiber. Try wrapping callbacks that you pass to non-Meteor libraries with Meteor.bindEnvironment.
W20150601-11:07:42.565(8)? (STDERR)     at Object.Meteor._nodeCodeMustBeInFiber (packages/meteor/dynamics_nodejs.js:9:1)
W20150601-11:07:42.565(8)? (STDERR)     at Object.Meteor.bindEnvironment (packages/meteor/dynamics_nodejs.js:85:1)

原因是我对结果的处理的方法pushApkRe.updatePushApkMsg 中更新数据库时是异步的,所以报错,因此按提示
 Try wrapping callbacks,对回调加上Meteor.bindEnvironent(),大功ok.

Meteor.methods({
    getApkMsg:function(_id,name){

       

        var storepath= "~/meteor_uploads"+"/"+"file_records"+"-"+_id+"-"+name;

        console.log("Meteor.methods storePath   "+storepath);

        var appinfo=parseApkFile(storepath,
Meteor.bindEnvironment(
function(msg){//parseApkFile解析方法 pushApkRe.updatePushApkMsg(_id,msg['packageName'],msg['versionName'])//对结果的处理 })); return appinfo; }});



 
 参考资料:http://stackoverflow.com/questions/18346847/future-wait-cant-wait-without-a-fiber-while-waiting-on-another-future-in-met


2. 第二种方法,服务器数据库监听obeser来做.

   client:

FileRe.insert(files[i], function (err, fileObj) {
    if (err) {
        alert('上传文件失败');
        return;
    }
  
    var url = FileRe.fileUrl(fileObj._id, fileObj.name());
    pushApk.addPushApk(fileObj.name(),'', '', url);
//callGetApkMsg(fileObj);//不再调用call()});

server:

pushApk.find({pkg:''}).observe({//pushApkRecords {createdAt: {$gte:0}} new Date().getTime()-1000*60*60*24*30*12
    added:function(record){
        console.log(" id: "+record._id+"record.name: "+record.name);
        var f=FileRe.findOne({_id:_id);
        parseApkFileMsg(f._id, f.name());
    }
});

var parseApkFileMsg=function(_id,name){

    var storepath= "~/meteor_uploads"+"/"+"file_records"+"-"+_id+"-"+name;
    var fs =Npm.require('fs');

    console.log("parseApkFileMsg storePath   "+storepath);

    var intervalHandle = Meteor.setInterval(function () {

        console.log("parseApkFileMsg  Meteor.setInterval");

        fs.open("/meteor_uploads"+"/"+"file_records"+"-"+_id+"-"+name,'r',Meteor.bindEnvironment(function(err,fd){//fs.open()只能用绝对路径
            if (err && err.code=='ENOENT') {
                console.log("parseApkFileMsg hasn't exists Inside interval---1000--------count:"+count);
            }else{
                parseApkFile(storepath,Meteor.bindEnvironment(function(msg){
                    console.log("--------Meteor.methods msg   "+msg+ " packageName :"+ msg['packageName'] + " versionName:"+msg['versionName']);
                    var url = FileRecords.fileUrl(_id,name);                  
                    pushApkRe.updatePushApkMsg(_id,msg['packageName'],msg['versionName'])//对结果的处理 

                }));

                console.log("parseApkFileMsg file has stored, close out interval");
                // file has stored, close out interval
                Meteor.clearInterval(intervalHandle);
            }
        }));

        //fs.exists(storepath, function (exists) {//unbuntu上这方法没有 改用fs.open()
        //    if(exists){
        //        console.log("parseApkFileMsg file has stored, close out interval");
        //    }else{
        //       console.log("parseApkFileMsg hasn't exists Inside interval---1000--------");
        //    }
        //
        //});

    }, 1000);


}


原文:http://blog.csdn.net/casun_li/article/details/46301027


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值