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.

114 lines
1.8 KiB

  1. /**
  2. * Module dependencies.
  3. */
  4. var EventEmitter = require('events').EventEmitter
  5. , parser = require('engine.io-parser')
  6. , debug = require('debug')('engine:transport');
  7. /**
  8. * Expose the constructor.
  9. */
  10. module.exports = Transport;
  11. /**
  12. * Noop function.
  13. *
  14. * @api private
  15. */
  16. function noop () {};
  17. /**
  18. * Transport constructor.
  19. *
  20. * @param {http.ServerRequest} request
  21. * @api public
  22. */
  23. function Transport (req) {
  24. this.readyState = 'opening';
  25. };
  26. /**
  27. * Inherits from EventEmitter.
  28. */
  29. Transport.prototype.__proto__ = EventEmitter.prototype;
  30. /**
  31. * Called with an incoming HTTP request.
  32. *
  33. * @param {http.ServerRequest} request
  34. * @api private
  35. */
  36. Transport.prototype.onRequest = function (req) {
  37. debug('setting request');
  38. this.req = req;
  39. };
  40. /**
  41. * Closes the transport.
  42. *
  43. * @api private
  44. */
  45. Transport.prototype.close = function (fn) {
  46. this.readyState = 'closing';
  47. this.doClose(fn || noop);
  48. };
  49. /**
  50. * Called with a transport error.
  51. *
  52. * @param {String} message error
  53. * @param {Object} error description
  54. * @api private
  55. */
  56. Transport.prototype.onError = function (msg, desc) {
  57. if (this.listeners('error').length) {
  58. var err = new Error(msg);
  59. err.type = 'TransportError';
  60. err.description = desc;
  61. this.emit('error', err);
  62. } else {
  63. debug('ignored transport error %s (%s)', msg, desc);
  64. }
  65. };
  66. /**
  67. * Called with parsed out a packets from the data stream.
  68. *
  69. * @param {Object} packet
  70. * @api private
  71. */
  72. Transport.prototype.onPacket = function (packet) {
  73. this.emit('packet', packet);
  74. };
  75. /**
  76. * Called with the encoded packet data.
  77. *
  78. * @param {String} data
  79. * @api private
  80. */
  81. Transport.prototype.onData = function (data) {
  82. this.onPacket(parser.decodePacket(data));
  83. };
  84. /**
  85. * Called upon transport close.
  86. *
  87. * @api private
  88. */
  89. Transport.prototype.onClose = function () {
  90. this.readyState = 'closed';
  91. this.emit('close');
  92. };