mm_connector
Version:
这是超级美眉游戏连接器,用于连接客户端和服务端,实现前后端安全通讯。
186 lines (168 loc) • 4.18 kB
JavaScript
const mosca = require('mosca');
require('./lib.js');
/**
* mqtt 物联网通讯类
*/
class MQTT {
/**
* 构造函数
* @param {Object} config 配置参数
*/
constructor(config) {
/**
* 配置参数
*/
this.config = Object.assign({
port: 1883,
cache: "mongodb",
cache_host: "mongodb://localhost:27017/mosca",
http: {
port: 8083,
bundle: true,
static: "./"
}
}, config);
/**
* MQTT服务器
*/
this.server = null;
}
}
/**
* 初始化
* @param {Object} config
*/
MQTT.prototype.init = function(config) {
if (config) {
this.config = Object.assign(this.config, config);
}
var cg = Object.assign({}, this.config);
if (cg.cache) {
cg.persistence = {
// 增加了此项
factory: cg.cache == "mongodb" ? mosca.persistence.Mongo : mosca.persistence.Redis,
url: cg.cache_host
}
}
this.server = new mosca.Server(cg);
this.middleware = new $.Middleware({
path: "./mqtt_middleware/".fullname($.runPath)
});
this.middleware.init(__dirname);
return this;
};
/**
* 引用
* @param {Function} 函数
*/
MQTT.prototype.use = function(func) {
// this.server.use(func);
};
/**
* 运行主程序
* @param {String} state 状态
*/
MQTT.prototype.main = function(state) {
var cg = this.config;
sr = this.server;
/**
* 对服务器端口进行配置,在此端口进行监听
* @param {Object} client 客户端信息
*/
sr.on('clientConnected', function(client) {
//监听连接
console.log('client connected', client.id);
});
var _this = this;
/**
* 监听MQTT主题消息
* @param {Object} packet 订阅消息
* @param {Object} client 客户端信息
*/
sr.on('published', function(packet, client) {
_this.published(packet, client);
// 当客户端有连接发布主题消息
// switch (packet.topic) {
// case 'test':
// console.log('message-publish', packet.payload.toString());
// break;
// case 'other':
// console.log('message-123', packet.payload.toString());
// break;
// }
});
/**
* 监听MQTT主题消息
* @param {String} topic 订阅主题
* @param {Object} client 客户端信息
*/
sr.on('subscribed', function(topic, client) {
_this.subscribed(topic, client);
});
/**
* 当服务开启时
*/
sr.on('ready', function() {
// 当服务开启时
console.info(`MQTT访问 mqtt://127.0.0.1:${cg.port}`);
});
sr.authenticate = this.auth;
}
/**
* 收到订阅时
* @param {String} topic 订阅主题
* @param {Object} client 客户端信息
*/
MQTT.prototype.subscribed = function(topic, client) {
// console.log("订阅", topic, client.id);
};
/**
* 收到推送消息时
* @param {Object} packet 订阅的消息
* @param {Object} client 客户端信息
*/
MQTT.prototype.published = function(packet, client) {
// console.log("发布", packet.topic, packet.payload.toString());
};
/**
* 身份验证
* @param {Object} client 客户端
* @param {String} username 用户名
* @param {String} password 密码
* @param {Function} callback 回调函数,回调返回true,则表示验证通过。
*/
MQTT.prototype.auth = function(client, username, password, callback) {
// console.log("连接授权", client.id, username, password ? password.toString() : '');
var bl = true;
// 回调第二个参数为true表示验证通过, 为false表示验证失败
callback(null, bl);
};
/**
* 运行主程序前
* @param {String} state 状态
*/
MQTT.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 状态
*/
MQTT.prototype.after = async function(state) {};
/**
* 运行
* @param {String} state 状态
*/
MQTT.prototype.run = async function(state = 'start') {
await this.before(state);
await this.main(state);
await this.after(state);
};
module.exports = MQTT;