throughputCounterQoS1.js 623 B

123456789101112131415161718192021222324252627282930313233
  1. #! /usr/bin/env node
  2. const mqtt = require('mqtt')
  3. const client = mqtt.connect({ port: 1883, host: 'localhost', clean: true, encoding: 'binary', keepalive: 0 })
  4. const interval = 5000
  5. let counter = 0
  6. function count () {
  7. console.log('received/s', counter / interval * 1000)
  8. counter = 0
  9. }
  10. setInterval(count, interval)
  11. client.on('connect', function () {
  12. this.subscribe('test', { qos: 1 })
  13. })
  14. client.handleMessage = function (packet, done) {
  15. counter++
  16. done()
  17. }
  18. client.on('offline', function () {
  19. console.log('offline')
  20. })
  21. client.on('error', function () {
  22. console.log('reconnect!')
  23. client.stream.end()
  24. })