原文: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 中更新数据库时是异步的,所以报错,因此按提示参考资料:http://stackoverflow.com/questions/18346847/future-wait-cant-wait-without-a-fiber-while-waiting-on-another-future-in-metTry 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,function(msg){//parseApkFile解析方法 pushApkRe.updatePushApkMsg(_id,msg['packageName'],msg['versionName'])//对结果的处理 })); return appinfo; }});Meteor.bindEnvironment(
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