1. 程式人生 > >mqtt------ mosca伺服器端引數簡介

mqtt------ mosca伺服器端引數簡介

一:伺服器端

為什麼使用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