server.js 723 B

12345678910111213141516171819202122232425
  1. 'use strict'
  2. // To be used with cpuprofilify http://npm.im/cpuprofilify
  3. const aedes = require('../')()
  4. const server = require('net').createServer(aedes.handle)
  5. const port = 1883
  6. server.listen(port, function () {
  7. console.error('server listening on port', port, 'pid', process.pid)
  8. })
  9. aedes.on('clientError', function (client, err) {
  10. console.error('client error', client.id, err.message)
  11. })
  12. // Cleanly shut down process on SIGTERM to ensure that perf-<pid>.map gets flushed
  13. process.on('SIGINT', onSIGINT)
  14. function onSIGINT () {
  15. // IMPORTANT to log on stderr, to not clutter stdout which is purely for data, i.e. dtrace stacks
  16. console.error('Caught SIGTERM, shutting down.')
  17. server.close()
  18. process.exit(0)
  19. }