transport.js 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160
  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. // SSL options for Node.js client
  29. this.pfx = opts.pfx;
  30. this.key = opts.key;
  31. this.passphrase = opts.passphrase;
  32. this.cert = opts.cert;
  33. this.ca = opts.ca;
  34. this.ciphers = opts.ciphers;
  35. this.rejectUnauthorized = opts.rejectUnauthorized;
  36. this.forceNode = opts.forceNode;
  37. // results of ReactNative environment detection
  38. this.isReactNative = opts.isReactNative;
  39. // other options for Node.js client
  40. this.extraHeaders = opts.extraHeaders;
  41. this.localAddress = opts.localAddress;
  42. }
  43. /**
  44. * Mix in `Emitter`.
  45. */
  46. Emitter(Transport.prototype);
  47. /**
  48. * Emits an error.
  49. *
  50. * @param {String} str
  51. * @return {Transport} for chaining
  52. * @api public
  53. */
  54. Transport.prototype.onError = function (msg, desc) {
  55. var err = new Error(msg);
  56. err.type = 'TransportError';
  57. err.description = desc;
  58. this.emit('error', err);
  59. return this;
  60. };
  61. /**
  62. * Opens the transport.
  63. *
  64. * @api public
  65. */
  66. Transport.prototype.open = function () {
  67. if ('closed' === this.readyState || '' === this.readyState) {
  68. this.readyState = 'opening';
  69. this.doOpen();
  70. }
  71. return this;
  72. };
  73. /**
  74. * Closes the transport.
  75. *
  76. * @api private
  77. */
  78. Transport.prototype.close = function () {
  79. if ('opening' === this.readyState || 'open' === this.readyState) {
  80. this.doClose();
  81. this.onClose();
  82. }
  83. return this;
  84. };
  85. /**
  86. * Sends multiple packets.
  87. *
  88. * @param {Array} packets
  89. * @api private
  90. */
  91. Transport.prototype.send = function (packets) {
  92. if ('open' === this.readyState) {
  93. this.write(packets);
  94. } else {
  95. throw new Error('Transport not open');
  96. }
  97. };
  98. /**
  99. * Called upon open
  100. *
  101. * @api private
  102. */
  103. Transport.prototype.onOpen = function () {
  104. this.readyState = 'open';
  105. this.writable = true;
  106. this.emit('open');
  107. };
  108. /**
  109. * Called with data.
  110. *
  111. * @param {String} data
  112. * @api private
  113. */
  114. Transport.prototype.onData = function (data) {
  115. var packet = parser.decodePacket(data, this.socket.binaryType);
  116. this.onPacket(packet);
  117. };
  118. /**
  119. * Called with a decoded packet.
  120. */
  121. Transport.prototype.onPacket = function (packet) {
  122. this.emit('packet', packet);
  123. };
  124. /**
  125. * Called upon close.
  126. *
  127. * @api private
  128. */
  129. Transport.prototype.onClose = function () {
  130. this.readyState = 'closed';
  131. this.emit('close');
  132. };