Meter 上传apk文件后,何时,如何调用服务器的解析方法 与结果处理
2015-06-01 10:53
344 查看
原文:/article/9867472.html
我现在的前提条件是已经用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); } 原文:/article/9867472.html
相关文章推荐
- 《Linux内核设计与实现》之中断与中断处理程序
- 使用Delphi实现JNI - 实例
- 什么是真正的实时操作系统
- Git PHP提交
- JDK 环境变量配置 -- Path classpath
- 电商术语
- MySQL性能优化之 延迟关联
- 微博背后的那些算法
- VS2010下如何改变Button颜色、背景、边框
- xbmc遥控音量加处理过程总结
- WEB安全之SQL注入
- SAP ERP项目各模块设计重点
- VB.net进阶培训及阶段总结
- What, exactly, is a Product Manager?
- sparkshell里的wordcount
- Linux基础:进程管理
- 如何在程序中写log日志(定义log级别:error,warn, info, debug; 宏定义打印不同级别的日志; 程序中引用宏定义即可)
- CSS阴影效果(Box-shadow)用法趣味讲解
- Spring3 MVC 注解
- 函数指针、函数对象、仿函数比较与入门