chenyc
2025-12-09 545c24c6a711d71b65f3d4e8122fee3837fb1edc
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
var instance = require('aedes')();
var server = require('net').createServer(instance.handle);
var port = 1883;
 
instance.subscribe('#', function(packet, cb) {
  const { topic } = packet;
  console.log("subscribe");
  console.log(packet);
  if (!topic.startsWith('$SYS') && !topic.endsWith('_reply')) {
    const message = JSON.parse(packet.payload.toString());
    const payload = JSON.stringify({
      id: message.id,
      code: 200
    });
    instance.publish({
      cmd: 'publish',
      qos: 0,
      topic: packet.topic + '_reply',
      payload: new Buffer(payload),
      retain: false
    });
  }
  cb();
});
 
 
// instance.keepaliveTimeout('#',(packet, cb)=>{
//   console.log("keepaliveTimeout");
//   cb();
// });
 
server.listen(port, function() {
  console.log('server listening on port', port);
});