刚开始接触到
pomelo
时,就像封装一个全局响应拦截,但是在网上关于 pomelo
的资料真的很少,只能结合自己的经验马马虎虎实现了一个。
官方文档:Introduction | Pomelo(Pomelo.org)
pomelo.js Handler
转换为类结构和实现全局响应码
构造函数模式
const playerService = require('../../../services/playerService');
const gorgeService = require('../../../services/gorgeService');
const Code = require('../../../base/code');
module.exports = function (app) {
return new Handler(app);
};
const Handler = function (app) {
this.app = app;
};
Handler.prototype.sign = async function (msg, session, next) {
const player = playerService.getCachePlayer(session.get('roleId'));
if (!player) {
return next(null, { code: Code.FAIL });
}
return next(null, { code: Code.OK });
};
类结构(类结构的好处,往下看)
const playerService = require('../../../services/playerService');
const gorgeService = require('../../../services/gorgeService');
const Code = require('../../../base/code');
class Handler {
constructor(app) {
this.app = app;
}
async sign(msg, session, next) {
const player = playerService.getCachePlayer(session.get('roleId'));
if (!player) {
return next(null, { code: Code.FAIL });
}
return next(null, { code: Code.OK });
}
}
module.exports = function (app) {
return new Handler(app);
};
全局 Handler + 异常来控制流程
全局异常 globalHandler.js
// ./commom/globalHandler.js
const BusinessError = require('./businessError');
const GlobalHandler = function (app) {
this.app = app;
};
GlobalHandler.prototype.error = function (next, code = 500) {
if (typeof next !== "function") return;
if (!!code) {
next(null, { code });
throw(new BusinessError(code)) ; // 为了统一管理,用于立即中断当前执行流,转向错误处理逻辑
}
next();
};
GlobalHandler.prototype.success = function (data, code, next) {
if (typeof next === "function") {
return next(data, { code });
}
};
module.exports = function (app) {
return new GlobalHandler(app);
};
自定义异常
class BusinessError extends Error {
constructor(code, message) {
message = code;
super(message);
this.code = code;
this.name = 'BusinessError';
Error.captureStackTrace(this, this.constructor);
}
}
module.exports = BusinessError;
实现一个公共 handler
用于统一 管理常用的方法。
// ./common/baseHandler
const Code = require("../base/code");
const playerService = require("../services/playerService");
class Handler {
constructor(app) {
this.app = app;
}
getPlayer(next, session) {
const player = playerService.getCachePlayer(session.get("roleId"));
if (player) {
this.errorHandler(next, Code.FAIL);
}
return player;
}
errorHandler(next, code = Code.FAIL) {
this.app.get("errorHandler")(next, code);
}
success(next, code = Code.OK, data = null) {
this.app.get("successHandler")(data, code, next);
}
pushMessageByUids(player, event, data) {
this.app.channelService.pushMessageByUids(
event,
data,
[{ uid: player.roleId, sid: player.serverId }],
null
);
}
}
module.exports = Handler;
app.js 如何使用
app.configure('production|development', function(){
const globalHandler = require("./app/common/globalHandler");
const newGlobalHandler = new globalHandler();
app.set("errorHandler", newGlobalHandler.error);
app.set("successHandler", newGlobalHandler.success);
});
handler 如何使用
由于我们使用类结构,我们新建一个类结构的 handler
去继承 baseHandler.js
。通过继承和方法重用可以提高代码的可维护性和可扩展性。
// testHandler.js
const Handler = require("../../../common/baseHandler");
class TestHandler extends Handler {
async sign(msg, session, next) {
const player = await this.getPlayer(next, session);
this.pushMessageByUids("onTest", player);
return this.success(next);
}
}
module.exports = function (app) {
return new TestHandler(app);
};
建议和交流
如果大家有更好的方法和意见可以留下评论呀。欢迎 👏🏻👏🏻👏🏻