transport.js 2.6 KB

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