一:服务器端
为什么使用mosca:mosca是基于node.js开发,上手难度相对较小,其次协议支持完整,除了不支持Qos 2,其它的基本都支持。持久化支持redis以及mongo。二次开发接口简单。部署非常简单,并且支持docker镜像。
mosca参数简介:
var mosca = require(‘mosca‘)
ascoltatore : 是Mosca作者开发的一个订阅与发布类库,Mosca核心的订阅与发布模型
var ascoltatore = { type: ‘redis‘, //指定类型,mongo的写法请参考官方wiki redis: require(‘redis‘), db: 1, port: 6379, return_buffers: true, // to handle binary payloads //指定数据保留多长时间,单位毫秒 ttl: { // TTL for subscriptions is 23 hour subscriptions: 23 * 60 * 60 * 1000, // TTL for packets is 23 hour packets: 23 * 60 * 60 * 1000, }, host: "localhost" };
//基本参数设置
var moscaSettings = { port: 1883, //设置监听端口 backend: ascoltatore, maxInflightMessages: 10240, //设置单条消息的最大长度,超出后服务端会返回 //设置WebSocket参数 http: { port: 1884, bundle: true, static: ‘./‘ }, //数据持久化参数设置 persistence: { factory: mosca.persistence.Redis, db: 1, port: 6379, return_buffers: true, // to handle binary payloads ttl: { // TTL for subscriptions is 23 hour subscriptions: 23 * 60 * 60 * 1000, // TTL for packets is 23 hour packets: 23 * 60 * 60 * 1000, }, host: "localhost" } }
//如果需要用户登录验证权限,需要改写此方法
//这里以简单判断了用户名和密码为例,真实环境可以连接实际业务系统的鉴权服务
var authenticate = function(client, username, password, callback) { var authorized = (username === ‘test‘ &;&; password.toString() === ‘passwd‘); if (authorized) client.user = username; callback(null, authorized); } function authPub(client, topic, payload, callback) { callback(null, payload);} function authSub(client, topic, callback) { callback(null, topic);} var server = new mosca.Server(moscaSettings); server.on(‘ready‘, setup); server.on(‘clientConnected‘, function(client) { console.log(‘client connected‘, client.id);}); server.on(‘published‘, function(packet, client) { console.log(‘Published‘, packet.topic + packet.payload);}); // fired when the mqtt server is ready function setup() { server.authenticate = authenticate; server.authorizePublish = authPub; server.authorizeSubscribe = authSub; console.log(‘Mosca server is up and running‘) }
二次开发可以监听的事件列表
clientConnected: when a client is connected; the client is passed as a parameter.
clientDisconnecting: when a client is being disconnected; the client is passed as a parameter.
clientDisconnected: when a client is disconnected; the client is passed as a parameter.
published: when a new message is published; the packet and the client are passed as parameters.
delivered: when a client has sent back a puback for a published message; the packet and the client are passed as parameters.
subscribed: when a client is subscribed to a topic; the topic and the client are passed as parameters.
unsubscribed: when a client is unsubscribed to a topic; the topic and the client are passed as parameters.
有了上面可以监听到事件你就可以根据自己的业务进行相应的开发,拦截特定的事件并添加业务代码
ascoltatore托管地址 https://github.com/mcollina/ascoltatori
高级参数设置可以参考 https://github.com/mcollina/mosca/wiki/Mosca-advanced-usage
权限验证可以参考 https://github.com/mcollina/mosca/wiki/Authentication-&;-Authorization
配置ssl可以参考 https://github.com/mcollina/mosca/wiki/TLS-SSL-Configuration
配置WebSocket可以参考 https://github.com/mcollina/mosca/wiki/MQTT-over-Websockets
原文地址:https://www.cnblogs.com/Joans/p/9957128.html