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.

150 lines
2.4 KiB

  1. /**
  2. * Module dependencies.
  3. */
  4. var parser = require('engine.io-parser');
  5. var Emitter = require('component-emitter');
  6. /**
  7. * Module exports.
  8. */
  9. module.exports = Transport;
  10. /**
  11. * Transport abstract constructor.
  12. *
  13. * @param {Object} options.
  14. * @api private
  15. */
  16. function Transport (opts) {
  17. this.path = opts.path;
  18. this.hostname = opts.hostname;
  19. this.port = opts.port;
  20. this.secure = opts.secure;
  21. this.query = opts.query;
  22. this.timestampParam = opts.timestampParam;
  23. this.timestampRequests = opts.timestampRequests;
  24. this.readyState = '';
  25. this.agent = opts.agent || false;
  26. this.socket = opts.socket;
  27. this.enablesXDR = opts.enablesXDR;
  28. }
  29. /**
  30. * Mix in `Emitter`.
  31. */
  32. Emitter(Transport.prototype);
  33. /**
  34. * A counter used to prevent collisions in the timestamps used
  35. * for cache busting.
  36. */
  37. Transport.timestamps = 0;
  38. /**
  39. * Emits an error.
  40. *
  41. * @param {String} str
  42. * @return {Transport} for chaining
  43. * @api public
  44. */
  45. Transport.prototype.onError = function (msg, desc) {
  46. var err = new Error(msg);
  47. err.type = 'TransportError';
  48. err.description = desc;
  49. this.emit('error', err);
  50. return this;
  51. };
  52. /**
  53. * Opens the transport.
  54. *
  55. * @api public
  56. */
  57. Transport.prototype.open = function () {
  58. if ('closed' == this.readyState || '' == this.readyState) {
  59. this.readyState = 'opening';
  60. this.doOpen();
  61. }
  62. return this;
  63. };
  64. /**
  65. * Closes the transport.
  66. *
  67. * @api private
  68. */
  69. Transport.prototype.close = function () {
  70. if ('opening' == this.readyState || 'open' == this.readyState) {
  71. this.doClose();
  72. this.onClose();
  73. }
  74. return this;
  75. };
  76. /**
  77. * Sends multiple packets.
  78. *
  79. * @param {Array} packets
  80. * @api private
  81. */
  82. Transport.prototype.send = function(packets){
  83. if ('open' == this.readyState) {
  84. this.write(packets);
  85. } else {
  86. throw new Error('Transport not open');
  87. }
  88. };
  89. /**
  90. * Called upon open
  91. *
  92. * @api private
  93. */
  94. Transport.prototype.onOpen = function () {
  95. this.readyState = 'open';
  96. this.writable = true;
  97. this.emit('open');
  98. };
  99. /**
  100. * Called with data.
  101. *
  102. * @param {String} data
  103. * @api private
  104. */
  105. Transport.prototype.onData = function(data){
  106. var packet = parser.decodePacket(data, this.socket.binaryType);
  107. this.onPacket(packet);
  108. };
  109. /**
  110. * Called with a decoded packet.
  111. */
  112. Transport.prototype.onPacket = function (packet) {
  113. this.emit('packet', packet);
  114. };
  115. /**
  116. * Called upon close.
  117. *
  118. * @api private
  119. */
  120. Transport.prototype.onClose = function () {
  121. this.readyState = 'closed';
  122. this.emit('close');
  123. };