-
Notifications
You must be signed in to change notification settings - Fork 0
/
mqtt.js
89 lines (75 loc) · 2.13 KB
/
mqtt.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
const mongo = require('mongodb');
const bmweb = require('bmweb');
const app = bmweb.app;
var createServer = function() {
var net = require('net');
var mqttCon = require('mqtt-connection');
var server = new net.Server();
server.on('connection', function(stream) {
var client = mqttCon(stream);
// client connected
client.on('connect', function(packet) {
// acknowledge the connect packet
client.connack({returnCode: 0});
});
// client published
client.on('publish', function(packet) {
// send a puback with messageId (for QoS > 0)
client.puback({messageId: packet.messageId});
});
// client pinged
client.on('pingreq', function() {
// send a pingresp
client.pingresp();
});
// client subscribed
client.on('subscribe', function(packet) {
// send a suback with messageId and granted QoS level
client.suback({granted: [packet.qos], messageId: packet.messageId});
});
// timeout idle streams after 5 minutes
stream.setTimeout(1000 * 60 * 5);
// connection error handling
client.on('close', function() {
client.destroy();
});
client.on('error', function() {
client.destroy();
});
client.on('disconnect', function() {
client.destroy();
});
// stream timeout
stream.on('timeout', function() {
client.destroy();
});
});
// listen on port 1883
server.listen(1883);
};
var createClient = function(callback) {
var net = require('net');
var mqttCon = require('mqtt-connection');
var stream = net.createConnection(1883, 'localhost');
var client = mqttCon(stream);
client.on('connect', function(err) {
console.log('line 36 connect', err);
callback(err, client);
});
client.on('message', function(topic, message) {
// message is Buffer
console.log('line 39', topic);
console.log('line 40', message.toString());
});
};
var exp = module.exports;
exp.start = function(callback) {
callback();
// createServer();
};
exp.afterStart = function(callback) {
callback();
// createClient(function(err, client) {
// console.log('line 99 connect ok');
// });
};