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.

157 lines
2.7 KiB

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