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.

245 lines
4.7 KiB

  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 debug = require('debug')('engine.io-client:polling');
  9. /**
  10. * Module exports.
  11. */
  12. module.exports = Polling;
  13. /**
  14. * Is XHR2 supported?
  15. */
  16. var hasXHR2 = (function() {
  17. var XMLHttpRequest = require('xmlhttprequest');
  18. var xhr = new XMLHttpRequest({ xdomain: false });
  19. return null != xhr.responseType;
  20. })();
  21. /**
  22. * Polling interface.
  23. *
  24. * @param {Object} opts
  25. * @api private
  26. */
  27. function Polling(opts){
  28. var forceBase64 = (opts && opts.forceBase64);
  29. if (!hasXHR2 || forceBase64) {
  30. this.supportsBinary = false;
  31. }
  32. Transport.call(this, opts);
  33. }
  34. /**
  35. * Inherits from Transport.
  36. */
  37. inherit(Polling, Transport);
  38. /**
  39. * Transport name.
  40. */
  41. Polling.prototype.name = 'polling';
  42. /**
  43. * Opens the socket (triggers polling). We write a PING message to determine
  44. * when the transport is open.
  45. *
  46. * @api private
  47. */
  48. Polling.prototype.doOpen = function(){
  49. this.poll();
  50. };
  51. /**
  52. * Pauses polling.
  53. *
  54. * @param {Function} callback upon buffers are flushed and transport is paused
  55. * @api private
  56. */
  57. Polling.prototype.pause = function(onPause){
  58. var pending = 0;
  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. var self = this;
  170. parser.encodePayload(packets, this.supportsBinary, function(data) {
  171. self.doWrite(data, callbackfn);
  172. });
  173. };
  174. /**
  175. * Generates uri for connection.
  176. *
  177. * @api private
  178. */
  179. Polling.prototype.uri = function(){
  180. var query = this.query || {};
  181. var schema = this.secure ? 'https' : 'http';
  182. var port = '';
  183. // cache busting is forced
  184. if (false !== this.timestampRequests) {
  185. query[this.timestampParam] = +new Date + '-' + Transport.timestamps++;
  186. }
  187. if (!this.supportsBinary && !query.sid) {
  188. query.b64 = 1;
  189. }
  190. query = parseqs.encode(query);
  191. // avoid port if default for schema
  192. if (this.port && (('https' == schema && this.port != 443) ||
  193. ('http' == schema && this.port != 80))) {
  194. port = ':' + this.port;
  195. }
  196. // prepend ? to query
  197. if (query.length) {
  198. query = '?' + query;
  199. }
  200. return schema + '://' + this.hostname + port + this.path + query;
  201. };