sub.js 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123
  1. #!/usr/bin/env node
  2. var mqtt = require('../')
  3. var path = require('path')
  4. var fs = require('fs')
  5. var helpMe = require('help-me')({
  6. dir: path.join(__dirname, '..', 'doc')
  7. })
  8. var minimist = require('minimist')
  9. function start (args) {
  10. args = minimist(args, {
  11. string: ['hostname', 'username', 'password', 'key', 'cert', 'ca', 'clientId', 'i', 'id'],
  12. boolean: ['stdin', 'help', 'clean', 'insecure'],
  13. alias: {
  14. port: 'p',
  15. hostname: ['h', 'host'],
  16. topic: 't',
  17. qos: 'q',
  18. clean: 'c',
  19. keepalive: 'k',
  20. clientId: ['i', 'id'],
  21. username: 'u',
  22. password: 'P',
  23. protocol: ['C', 'l'],
  24. verbose: 'v',
  25. help: '-H',
  26. ca: 'cafile'
  27. },
  28. default: {
  29. host: 'localhost',
  30. qos: 0,
  31. retain: false,
  32. clean: true,
  33. keepAlive: 30 // 30 sec
  34. }
  35. })
  36. if (args.help) {
  37. return helpMe.toStdout('subscribe')
  38. }
  39. args.topic = args.topic || args._.shift()
  40. if (!args.topic) {
  41. console.error('missing topic\n')
  42. return helpMe.toStdout('subscribe')
  43. }
  44. if (args.key) {
  45. args.key = fs.readFileSync(args.key)
  46. }
  47. if (args.cert) {
  48. args.cert = fs.readFileSync(args.cert)
  49. }
  50. if (args.ca) {
  51. args.ca = fs.readFileSync(args.ca)
  52. }
  53. if (args.key && args.cert && !args.protocol) {
  54. args.protocol = 'mqtts'
  55. }
  56. if (args.insecure) {
  57. args.rejectUnauthorized = false
  58. }
  59. if (args.port) {
  60. if (typeof args.port !== 'number') {
  61. console.warn('# Port: number expected, \'%s\' was given.', typeof args.port)
  62. return
  63. }
  64. }
  65. if (args['will-topic']) {
  66. args.will = {}
  67. args.will.topic = args['will-topic']
  68. args.will.payload = args['will-message']
  69. args.will.qos = args['will-qos']
  70. args.will.retain = args['will-retain']
  71. }
  72. args.keepAlive = args['keep-alive']
  73. var client = mqtt.connect(args)
  74. client.on('connect', function () {
  75. client.subscribe(args.topic, { qos: args.qos }, function (err, result) {
  76. if (err) {
  77. console.error(err)
  78. process.exit(1)
  79. }
  80. result.forEach(function (sub) {
  81. if (sub.qos > 2) {
  82. console.error('subscription negated to', sub.topic, 'with code', sub.qos)
  83. process.exit(1)
  84. }
  85. })
  86. })
  87. })
  88. client.on('message', function (topic, payload) {
  89. if (args.verbose) {
  90. console.log(topic, payload.toString())
  91. } else {
  92. console.log(payload.toString())
  93. }
  94. })
  95. client.on('error', function (err) {
  96. console.warn(err)
  97. client.end()
  98. })
  99. }
  100. module.exports = start
  101. if (require.main === module) {
  102. start(process.argv.slice(2))
  103. }