欢迎访问生活随笔!

生活随笔

您现在的位置是:首页 > 形式科学 > 计算机科学 > IT网络

IT网络

Pomelo热更新刷新处理程序和remote和Pomelo使用bearcat进行热更新。

发布时间:2022-11-21IT网络 小博士
1.开启原生pomelo的hotreload支持
Pomelo版本:2.2.5,通过添加以下代码编辑脚本app . js//全局配置app.configure('production|dev

一. 开启 原生 pomelo 的hotreload支持

pomelo版本: 2.2.5 , 编辑脚本 app.js 加入如下代码

//全局配置 app.configure('production|development', function() { //让所有服务器 都支持 handle 和remote 热更新 let serverConfig = { 'reloadHandlers':true, 'reloadRemotes':true, }; app.set('serverConfig',serverConfig); });

原理:监听文件改动,在文件变化以后重新加载,只能更新 remote rpc 和 handler

//监听 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 文件里不要保存局部数据,否则刷新以后会丢失.

二 .使用bearcat 热更新

参考项目 https://github.com/NetEase/treasures

根据 treasures 配置好 context.json

{ "name": "bearcat", "scan": "app", "beans": [] }

scan 就是要检测的目录 .

修改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重新加载.

三. 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 更新主要是更新 新创建的对象! 以前的对象是使用以前的代码,这种情况是更新不了的!