This repository has been archived by the owner on Feb 11, 2020. It is now read-only.
-
-
Notifications
You must be signed in to change notification settings - Fork 506
/
Copy pathServer_With_All_Interfaces-Settings.js
93 lines (73 loc) · 2.59 KB
/
Server_With_All_Interfaces-Settings.js
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
82
83
84
85
86
87
88
89
90
91
92
93
var mosca = require('mosca');
var pubsubSettings = {
/* For AMQP */
type: 'amqp',
json: false,
amqp: require('amqp'),
exchange: 'amq.topic'
};
var SECURE_KEY = __dirname + '/../../test/secure/tls-key.pem';
var SECURE_CERT = __dirname + '/../../test/secure/tls-cert.pem';
var moscaSetting = {
interfaces: [
{ type: "mqtt", port: 1883 },
{ type: "mqtts", port: 8883, credentials: { keyPath: SECURE_KEY, certPath: SECURE_CERT } },
{ type: "http", port: 3000, bundle: true },
{ type: "https", port: 3001, bundle: true, credentials: { keyPath: SECURE_KEY, certPath: SECURE_CERT } }
],
stats: false,
onQoS2publish: 'noack', // can set to 'disconnect', or to 'dropToQoS1' if using a client which will eat puback for QOS 2; e.g. mqtt.js
logger: { name: 'MoscaServer', level: 'debug' },
persistence: { factory: mosca.persistence.Redis, url: 'localhost:6379', ttl: { subscriptions: 1000 * 60 * 10, packets: 1000 * 60 * 10 } },
backend: pubsubSettings,
};
var authenticate = function (client, username, password, callback) {
if (username == "test" && password.toString() == "test")
callback(null, true);
else
callback(null, false);
}
var authorizePublish = function (client, topic, payload, callback) {
var auth = true;
// set auth to :
// true to allow
// false to deny and disconnect
// 'ignore' to puback but not publish msg.
callback(null, auth);
}
var authorizeSubscribe = function (client, topic, callback) {
var auth = true;
// set auth to :
// true to allow
// false to deny
callback(null, auth);
}
var server = new mosca.Server(moscaSetting);
server.on('ready', setup);
function setup() {
server.authenticate = authenticate;
server.authorizePublish = authorizePublish;
server.authorizeSubscribe = authorizeSubscribe;
console.log('Mosca server is up and running.');
}
server.on("error", function (err) {
console.log(err);
});
server.on('clientConnected', function (client) {
console.log('Client Connected \t:= ', client.id);
});
server.on('published', function (packet, client) {
console.log("Published :=", packet);
});
server.on('subscribed', function (topic, client) {
console.log("Subscribed :=", client.packet);
});
server.on('unsubscribed', function (topic, client) {
console.log('unsubscribed := ', topic);
});
server.on('clientDisconnecting', function (client) {
console.log('clientDisconnecting := ', client.id);
});
server.on('clientDisconnected', function (client) {
console.log('Client Disconnected := ', client.id);
});