(B) Based on the service end message to push mqtt

Based on the service side implementation Mosca

About Mosca, Mosca quoted from Home project development based on node.js, characteristics are described below:

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)

Of particular note is Mosca does not support Qos 2.

Why Mosca? First, based on node.js development, is relatively easy to get started, followed by the full protocol support, in addition to not support Qos 2, other basic support. Persistence support redis and mongo. Secondary development interface simple. Very simple to deploy, and support docker mirror.

Development steps

Installation Mosca

This article assumes that you have already installed environment node.js environment and redis, my native environment as follows: 10.11.2 node.js v0.12 redis latest version of mac ox.

Mosca Project Hosting Address: https://github.com/mcollina/mosca
official Wiki: https://github.com/mcollina/mosca/wiki

Installation is very simple, quoted official sources as follows:

Standalone

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

Embedded

1
npm install mosca --save

Startup script

The official gave a simple example, the following is my test code now in use:

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
Mosca = var The require ( 'Mosca') 

// ascoltatore Mosca is developed by the authors and publishing a subscription library, Mosca core subscription and publishing model
var = {ascoltatore
of the type: 'Redis', // specify the type, mongo wording please refer to the official Wiki
Redis: the require ( 'Redis'),
DB: . 1,
Port: 6379,
return_buffers: to true, // payloads to handle binary // specify how long data retention milliseconds TTL: { // for the TTL IS Subscriptions hour 23 subscriptions: 23 * 60 * 60 * 1000, // 23 hour TTL for packets IS packets: 23 * 60 * 60 * 1000, }, Host:







"localhost"
};

// set the basic parameters
var moscaSettings = {
Port: 1883, // set listening port
backend: ascoltatore,
maxInflightMessages: 10240, // set the maximum length of a single message, the server might return beyond the // set WebSocket parameters HTTP: { Port: 1884, the bundle: to true, static: './' }, // data persistence parameter persistence: { Factory: mosca.persistence.Redis, DB: . 1, Port: 6379, return_buffers: to true, to handle binary payloads // ttl: { // TTL for subscriptions IS 23 hour subscriptions: 23 * 60 *














* 60 1000, @ 23 hour TTL for packets IS packets: 23 * 60 * 60 * 1000, }, Host: "localhost" } }; // If a user login authentication authority, need to override this method // here a simple Analyzing the user name and password, for example, the actual real-world business system can be connected to the authentication service var = the authenticate function ( Client, username, password, the callback) { var Authorized = (username === 'Test' && password.toString () === 'the passwd'); IF (Authorized) client.user = username; the callback ( null, Authorized); } function authPub ( Client, Topic, payload, the callback) { the 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')
}

Secondary development

You can listen to the list of events

  • 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/

 

Guess you like

Origin www.cnblogs.com/youyong/p/11388384.html