UNPKG

mm_connector

Version:

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

183 lines (163 loc) 3.79 kB
require('mm_expand'); require('mm_logs'); require('mm_ret'); require('./mod.js'); const mosca = require('mosca'); var WEB = require("./core/web"); var MQTT = require("./core/mqtt"); const Com = require('./com'); var com = new Com(); /** * 超级美眉连接器类 */ class MM_connector { /** * 构造函数 * @param {Object} config 配置参数 */ constructor(config) { /** * 配置参数 */ this.config = Object.assign({ sys: { // 服务端名称 name: "mm", // 服务端中文名 title: "超级美眉", // 缓存方式 cache: "memory", // 系统使用的语言 lang: "zh_CN" }, web: { state: true, // 访问地址 host: "0.0.0.0", // 访问端口号 port: 5000, // 是否启用websocket socket: true, // 是否启用压缩 compress: true, // 是否启用事件 event: true, // 是否启用事件 log: true, // 是否启用静态文件 static: true, // 代理转发 proxy: {} }, mqtt: { state: true, // mqtt访问端口号 port: 1883, // websocket 访问端口 http: { port: 8083 }, // 缓存方式 cache: "mongodb", // 缓存服务器地址 cache_host: "mongodb://localhost:27017/mosca" } }, config); this.mqtt = null; this.web = null; } } MM_connector.prototype.loadMod = function(cg) { var sys = cg.sys; // 选择缓存方式,默认memory缓存 if (sys.cache === 'redis') { // 将Api的缓存改为redis方式,如果不用redis可以将以下4行注释掉 var redis = $.redis_admin('sys'); redis.setConfig(cg.redis); redis.open(); $.cache = redis; } else if (sys.cache === 'cache') { // 将Api的缓存改为cache方式, 本地缓存方式 $.cache_admin = require('mm_cache').cache_admin; $.push($.cache, $.cache_admin('sys'), true); } else if (sys.cache === 'mongodb') { var mongodb = $.mongodb_admin('sys'); mongodb.setConfig(cg.mongodb); mongodb.open(); $.cache = mongodb; } $.sql = $.mysql_admin('sys', __dirname); $.sql.setConfig(cg.mysql); $.sql.open(); // 创建一个任务管理器 $.app = $.app_admin('app', '系统应用'); $.app.update(); $.app.init(); } /** * 初始化 * @param {Object} config 配置参数 */ MM_connector.prototype.init = function(config) { if (config) { this.config = Object.assign(this.config, config); } var cg = this.config; // 引用机制库 com.init(); // 加载全局功能模块 this.loadMod(cg); if (cg.web && cg.web.state) { this.web = new WEB(cg.web); this.web.init(); } if (cg.mqtt && cg.mqtt.state) { this.mqtt = new MQTT(cg.mqtt); this.mqtt.init(); } return this; }; /** * 运行主程序 * @param {String} state 状态 */ MM_connector.prototype.main = async function(state) { var cg = this.config; var web_server = this.web_server; var mqtt_server = this.mqtt_server; var tip = "启动"; if (cg.web && cg.web.state) { tip += " web"; if (cg.web.socket) { tip += " socket"; } this.web.run(); } if (cg.mqtt && cg.mqtt.state) { this.mqtt.run(); tip += " mqtt"; } $.log.info(tip); }; /** * 运行主程序前 * @param {String} state 状态 */ MM_connector.prototype.before = async function(state) { $.app.run(); }; /** * 运行主程序后 * @param {String} state 状态 */ MM_connector.prototype.after = async function(state) {}; /** * 运行 * @param {String} state 状态 */ MM_connector.prototype.run = async function(state = 'start') { await this.before(state); await this.main(state); await this.after(state); }; module.exports = MM_connector;