bombingQoS1.js 650 B

123456789101112131415161718192021222324252627282930313233343536
  1. #! /usr/bin/env node
  2. const mqtt = require('mqtt')
  3. const client = mqtt.connect({ port: 1883, host: 'localhost', clean: true, keepalive: 0 })
  4. const interval = 5000
  5. let sent = 0
  6. function count () {
  7. console.log('sent/s', sent / interval * 1000)
  8. sent = 0
  9. }
  10. setInterval(count, interval)
  11. function publish () {
  12. sent++
  13. client.publish('test', 'payload', { qos: 1 }, publish)
  14. }
  15. client.setMaxListeners(100)
  16. client.on('connect', function () {
  17. for (let i = 0; i < 50; i++) {
  18. publish()
  19. }
  20. })
  21. client.on('offline', function () {
  22. console.log('offline')
  23. })
  24. client.on('error', function () {
  25. console.log('reconnect!')
  26. client.stream.end()
  27. })