UNPKG

mm_connector

Version:

这是超级美眉游戏连接器,用于连接客户端和服务端,实现前后端安全通讯。

106 lines (96 loc) 1.96 kB
const Koa = require('koa'); /** * Web http 通讯类 */ class WEB { /** * 构造函数 * @param {Object} config 配置参数 */ constructor(config) { /** * 配置参数 */ this.config = Object.assign({ host: "0.0.0.0", port: 5000, // 是否启用压缩 compress: true, socket: true, event: true, log: true, static: true, static_path: "./static".fullname($.runPath), proxy: {} }, config); /** * web服务器 */ this.server = null; } } /** * 初始化 * @param {Object} config */ WEB.prototype.init = function(config) { if (config) { this.config = Object.assign(this.config, config); } this.server = new Koa(); this.middleware = new $.Middleware({ path: "./web_middleware/".fullname($.runPath) }); this.middleware.init(__dirname); return this; }; /** * 引用 * @param {Function} 函数 */ WEB.prototype.use = function(func) { this.server.use(func); }; /** * 运行主程序 * @param {String} state 状态 */ WEB.prototype.main = function(state) { var cg = this.config; var host = cg.host; if (host == '0.0.0.0') { host = '127.0.0.1' } this.server.listen(cg.port, cg.host, () => { console.info(`HTTP访问 http://${host}:${cg.port}`); }); }; /** * 运行主程序前 * @param {String} state 状态 */ WEB.prototype.before = async function(state) { var list = this.middleware.list; for (var i = 0; i < list.length; i++) { var o = list[i]; o.func = require(o.func_file); if (o.func) { o.func(this.server, this.config); } } }; /** * 运行主程序后 * @param {String} state 状态 */ WEB.prototype.after = async function(state) {}; /** * 运行 * @param {String} state 状态 */ WEB.prototype.run = async function(state = 'start') { await this.before(state); await this.main(state); await this.after(state); }; module.exports = WEB;