You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

35 lines
1.5 KiB

  1. /* jshint esversion: 6 */
  2. const WebSocketServer = require('ws').Server;
  3. const NetfluxSrv = require('chainpad-server');
  4. module.exports.create = function (config) {
  5. // asynchronously create a historyKeeper and RPC together
  6. require('./historyKeeper.js').create(config, function (err, historyKeeper) {
  7. if (err) { throw err; }
  8. var log = config.log;
  9. // spawn ws server and attach netflux event handlers
  10. NetfluxSrv.create(new WebSocketServer({ server: config.httpServer}))
  11. .on('channelClose', historyKeeper.channelClose)
  12. .on('channelMessage', historyKeeper.channelMessage)
  13. .on('channelOpen', historyKeeper.channelOpen)
  14. .on('sessionClose', historyKeeper.sessionClose)
  15. .on('error', function (error, label, info) {
  16. if (!error) { return; }
  17. if (['EPIPE', 'ECONNRESET'].indexOf(error && error.code) !== -1) { return; }
  18. /* labels:
  19. SEND_MESSAGE_FAIL, SEND_MESSAGE_FAIL_2, FAIL_TO_DISCONNECT,
  20. FAIL_TO_TERMINATE, HANDLE_CHANNEL_LEAVE, NETFLUX_BAD_MESSAGE,
  21. NETFLUX_WEBSOCKET_ERROR, NF_ENOENT
  22. */
  23. log.error(label, {
  24. code: error.code,
  25. message: error.message,
  26. stack: error.stack,
  27. info: info,
  28. });
  29. })
  30. .register(historyKeeper.id, historyKeeper.directMessage);
  31. });
  32. };