-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmqtt-server.js
62 lines (47 loc) · 1.45 KB
/
mqtt-server.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
"use strict";
var mqtt = require('mqtt');
var KEY = __dirname + '/ssl/agent1-key.pem';
var CERT = __dirname + '/ssl/agent1-cert.pem';
var PORT = 8443;
console.log("-- Starting secure server on " + PORT + " --");
var server = mqtt.createSecureServer(KEY, CERT, function (client) {
var self = this;
if (!self.clients) {
self.clients = {};
}
client.on('connect', function (packet) {
client.connack({returnCode: 0});
client.id = packet.clientId;
self.clients[client.id] = client;
console.log("Client connected", client.id);
});
client.on('publish', function (packet) {
var k;
for (k in self.clients) {
if (self.clients.hasOwnProperty(k)) {
self.clients[k].publish({topic: packet.topic, payload: packet.payload});
}
}
});
client.on('subscribe', function (packet) {
client.suback({
messageId: packet.messageId,
granted: packet.subscriptions.map(function (e) {
return e.qos;
})
});
});
client.on('pingreq', function (packet) {
client.pingresp();
});
client.on('disconnect', function (packet) {
client.stream.end();
});
client.on('close', function (err) {
delete self.clients[client.id];
});
client.on('error', function (err) {
client.stream.end();
console.log('error!', err);
});
}).listen(PORT);