polling.js 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245
  1. /**
  2. * Module dependencies.
  3. */
  4. var Transport = require('../transport');
  5. var parseqs = require('parseqs');
  6. var parser = require('engine.io-parser');
  7. var inherit = require('component-inherit');
  8. var yeast = require('yeast');
  9. var debug = require('debug')('engine.io-client:polling');
  10. /**
  11. * Module exports.
  12. */
  13. module.exports = Polling;
  14. /**
  15. * Is XHR2 supported?
  16. */
  17. var hasXHR2 = (function () {
  18. var XMLHttpRequest = require('xmlhttprequest-ssl');
  19. var xhr = new XMLHttpRequest({ xdomain: false });
  20. return null != xhr.responseType;
  21. })();
  22. /**
  23. * Polling interface.
  24. *
  25. * @param {Object} opts
  26. * @api private
  27. */
  28. function Polling (opts) {
  29. var forceBase64 = (opts && opts.forceBase64);
  30. if (!hasXHR2 || forceBase64) {
  31. this.supportsBinary = false;
  32. }
  33. Transport.call(this, opts);
  34. }
  35. /**
  36. * Inherits from Transport.
  37. */
  38. inherit(Polling, Transport);
  39. /**
  40. * Transport name.
  41. */
  42. Polling.prototype.name = 'polling';
  43. /**
  44. * Opens the socket (triggers polling). We write a PING message to determine
  45. * when the transport is open.
  46. *
  47. * @api private
  48. */
  49. Polling.prototype.doOpen = function () {
  50. this.poll();
  51. };
  52. /**
  53. * Pauses polling.
  54. *
  55. * @param {Function} callback upon buffers are flushed and transport is paused
  56. * @api private
  57. */
  58. Polling.prototype.pause = function (onPause) {
  59. var self = this;
  60. this.readyState = 'pausing';
  61. function pause () {
  62. debug('paused');
  63. self.readyState = 'paused';
  64. onPause();
  65. }
  66. if (this.polling || !this.writable) {
  67. var total = 0;
  68. if (this.polling) {
  69. debug('we are currently polling - waiting to pause');
  70. total++;
  71. this.once('pollComplete', function () {
  72. debug('pre-pause polling complete');
  73. --total || pause();
  74. });
  75. }
  76. if (!this.writable) {
  77. debug('we are currently writing - waiting to pause');
  78. total++;
  79. this.once('drain', function () {
  80. debug('pre-pause writing complete');
  81. --total || pause();
  82. });
  83. }
  84. } else {
  85. pause();
  86. }
  87. };
  88. /**
  89. * Starts polling cycle.
  90. *
  91. * @api public
  92. */
  93. Polling.prototype.poll = function () {
  94. debug('polling');
  95. this.polling = true;
  96. this.doPoll();
  97. this.emit('poll');
  98. };
  99. /**
  100. * Overloads onData to detect payloads.
  101. *
  102. * @api private
  103. */
  104. Polling.prototype.onData = function (data) {
  105. var self = this;
  106. debug('polling got data %s', data);
  107. var callback = function (packet, index, total) {
  108. // if its the first message we consider the transport open
  109. if ('opening' === self.readyState) {
  110. self.onOpen();
  111. }
  112. // if its a close packet, we close the ongoing requests
  113. if ('close' === packet.type) {
  114. self.onClose();
  115. return false;
  116. }
  117. // otherwise bypass onData and handle the message
  118. self.onPacket(packet);
  119. };
  120. // decode payload
  121. parser.decodePayload(data, this.socket.binaryType, callback);
  122. // if an event did not trigger closing
  123. if ('closed' !== this.readyState) {
  124. // if we got data we're not polling
  125. this.polling = false;
  126. this.emit('pollComplete');
  127. if ('open' === this.readyState) {
  128. this.poll();
  129. } else {
  130. debug('ignoring poll - transport state "%s"', this.readyState);
  131. }
  132. }
  133. };
  134. /**
  135. * For polling, send a close packet.
  136. *
  137. * @api private
  138. */
  139. Polling.prototype.doClose = function () {
  140. var self = this;
  141. function close () {
  142. debug('writing close packet');
  143. self.write([{ type: 'close' }]);
  144. }
  145. if ('open' === this.readyState) {
  146. debug('transport open - closing');
  147. close();
  148. } else {
  149. // in case we're trying to close while
  150. // handshaking is in progress (GH-164)
  151. debug('transport not open - deferring close');
  152. this.once('open', close);
  153. }
  154. };
  155. /**
  156. * Writes a packets payload.
  157. *
  158. * @param {Array} data packets
  159. * @param {Function} drain callback
  160. * @api private
  161. */
  162. Polling.prototype.write = function (packets) {
  163. var self = this;
  164. this.writable = false;
  165. var callbackfn = function () {
  166. self.writable = true;
  167. self.emit('drain');
  168. };
  169. parser.encodePayload(packets, this.supportsBinary, function (data) {
  170. self.doWrite(data, callbackfn);
  171. });
  172. };
  173. /**
  174. * Generates uri for connection.
  175. *
  176. * @api private
  177. */
  178. Polling.prototype.uri = function () {
  179. var query = this.query || {};
  180. var schema = this.secure ? 'https' : 'http';
  181. var port = '';
  182. // cache busting is forced
  183. if (false !== this.timestampRequests) {
  184. query[this.timestampParam] = yeast();
  185. }
  186. if (!this.supportsBinary && !query.sid) {
  187. query.b64 = 1;
  188. }
  189. query = parseqs.encode(query);
  190. // avoid port if default for schema
  191. if (this.port && (('https' === schema && Number(this.port) !== 443) ||
  192. ('http' === schema && Number(this.port) !== 80))) {
  193. port = ':' + this.port;
  194. }
  195. // prepend ? to query
  196. if (query.length) {
  197. query = '?' + query;
  198. }
  199. var ipv6 = this.hostname.indexOf(':') !== -1;
  200. return schema + '://' + (ipv6 ? '[' + this.hostname + ']' : this.hostname) + port + this.path + query;
  201. };