一. 开启 原生 pomelo 的hotreload支持
-
pomelo版本: 2.2.5 , 编辑脚本 app.js 加入以下代码node
//全局配置 app.configure('production|development', function() { //让全部服务器 都支持 handle 和remote 热更新 let serverConfig = { 'reloadHandlers':true, 'reloadRemotes':true, }; app.set('serverConfig',serverConfig); });
-
原理:监听文件改动,在文件变化之后从新加载,只能更新 remote rpc 和 handlergit
//监听 handler var watchHandlers = function(app, handlerMap) { var p = pathUtil.getHandlerPath(app.getBase(), app.serverType); if (!!p){ fs.watch(p, function(event, name) { if(event === 'change') { handlerMap[app.serverType] = Loader.load(p, app); } }); } };
注意: 在 remote和handler 文件里不要保存局部数据,不然刷新之后会丢失.github
二 .使用bearcat 热更新
-
参考项目 https://github.com/NetEase/treasuresjson
-
根据 treasures 配置好 context.json服务器
{ "name": "bearcat", "scan": "app", "beans": [] }
scan 就是要检测的目录 .session
- 修改app.js,添加以下内容
//BEARCAT_HOT 必定要配置成 on //BEARCAT_LOGGER 若是不关闭,则pomelo的日志输出 会将全部的日志都输出到 pomelo-undefined.log 里面. var contextPath = require.resolve('./context.json'); bearcat.createApp([contextPath], { BEARCAT_HOT: 'on',// 开启热更新,若是是off 那么不会热更新 BEARCAT_LOGGER: 'off',//setup 'off' to turn off bearcat logger configuration, } ); // 启动APP // app.start(); bearcat.start(function() { app.set('bearcat', bearcat); // start app app.start(); });
经过上面简单的操做,bearcat 就已经配置好了.试一试修改文件,控制台就会有提示bearcat从新加载.app
三. bearcat更新示例
entryHandler.js
Handler.prototype.enter = function (msg, session, next) { //这行若是写在函数外面,则不能热更新,每次修改文件要生效,都必须从新加载文件 var Student = require('./student'); var _stu = new Student(); _stu.say(); next(null,0); return; }
student.js
var Student = function(){ } Student.prototype.say = function(){ console.log("i am old say"); console.log("i am old say"); console.log("i am old say"); console.log("i am old say"); } module.exports = function(){ return new Student(); }
测试过程:函数
- 开启服务器 node app.js
- 调用 connector.entryHandler.enter , 会打印 i am old say
- 修改 i am old say ==> i am new say
- 控制台输出正在 reload ,等待reload 完毕.
- 调用 connector.entryHandler.enter , 会打印 i am new say
注意:
-
这行若是写在函数外面,则不能热更新,每次修改文件要生效,都必须从新加载文件 var Student = require('./student');测试
-
说明 bearcat 更新主要是更新 新建立的对象! 之前的对象是使用之前的代码,这种状况是更新不了的!ui