基於mqtt的消息推送(二)服務端實現


基於Mosca的服務端實現

Mosca簡介,Mosca基於node.js開發,特性引用自項目首頁介紹如下:

Features

  • MQTT 3.1 and 3.1.1 compliant.
  • QoS 0 and QoS 1.
  • Various storage options for QoS 1 offline packets, and subscriptions.
  • As fast as it is possible.
  • Usable inside ANY other Node.js app.
  • Supports Node.js v0.10 and v0.12.
  • Supports io.js v1.x and v2.x and v3.x (please do not use v3.1.0)

需要特別說明的是Mosca並不支持Qos 2。

為什么選擇Mosca?首先基於node.js開發,上手難度相對較小,其次協議支持完整,除了不支持Qos 2,其它的基本都支持。持久化支持redis以及mongo。二次開發接口簡單。部署非常簡單,並且支持docker鏡像。

開發步驟

安裝Mosca

本文環境假設你已經安裝了node.js環境以及redis,我本機的環境如下:mac ox 10.11.2 node.js v0.12 redis最新版本。

Mosca項目托管地址:https://github.com/mcollina/mosca
官方wiki:https://github.com/mcollina/mosca/wiki

安裝非常簡單,源引自官方說明如下:

Standalone

1
2
npm install mosca bunyan -g
mosca -v | bunyan

Embedded

1
npm install mosca --save

啟動腳本

官方給了一個簡單的例子,如下是我現在使用的測試代碼:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
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

寫在最后

感謝Mosca的作者mcollina,Mosca非常強大,並且足夠簡單。下篇文章會介紹如何利用IOS和Android開源客戶端類庫與Mosca對接,敬請期待!

 


轉載: http://targe.me/2015/12/29/mqtt-second/

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM