polling.js 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247
  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 pending = 0;
  60. var self = this;
  61. this.readyState = 'pausing';
  62. function pause(){
  63. debug('paused');
  64. self.readyState = 'paused';
  65. onPause();
  66. }
  67. if (this.polling || !this.writable) {
  68. var total = 0;
  69. if (this.polling) {
  70. debug('we are currently polling - waiting to pause');
  71. total++;
  72. this.once('pollComplete', function(){
  73. debug('pre-pause polling complete');
  74. --total || pause();
  75. });
  76. }
  77. if (!this.writable) {
  78. debug('we are currently writing - waiting to pause');
  79. total++;
  80. this.once('drain', function(){
  81. debug('pre-pause writing complete');
  82. --total || pause();
  83. });
  84. }
  85. } else {
  86. pause();
  87. }
  88. };
  89. /**
  90. * Starts polling cycle.
  91. *
  92. * @api public
  93. */
  94. Polling.prototype.poll = function(){
  95. debug('polling');
  96. this.polling = true;
  97. this.doPoll();
  98. this.emit('poll');
  99. };
  100. /**
  101. * Overloads onData to detect payloads.
  102. *
  103. * @api private
  104. */
  105. Polling.prototype.onData = function(data){
  106. var self = this;
  107. debug('polling got data %s', data);
  108. var callback = function(packet, index, total) {
  109. // if its the first message we consider the transport open
  110. if ('opening' == self.readyState) {
  111. self.onOpen();
  112. }
  113. // if its a close packet, we close the ongoing requests
  114. if ('close' == packet.type) {
  115. self.onClose();
  116. return false;
  117. }
  118. // otherwise bypass onData and handle the message
  119. self.onPacket(packet);
  120. };
  121. // decode payload
  122. parser.decodePayload(data, this.socket.binaryType, callback);
  123. // if an event did not trigger closing
  124. if ('closed' != this.readyState) {
  125. // if we got data we're not polling
  126. this.polling = false;
  127. this.emit('pollComplete');
  128. if ('open' == this.readyState) {
  129. this.poll();
  130. } else {
  131. debug('ignoring poll - transport state "%s"', this.readyState);
  132. }
  133. }
  134. };
  135. /**
  136. * For polling, send a close packet.
  137. *
  138. * @api private
  139. */
  140. Polling.prototype.doClose = function(){
  141. var self = this;
  142. function close(){
  143. debug('writing close packet');
  144. self.write([{ type: 'close' }]);
  145. }
  146. if ('open' == this.readyState) {
  147. debug('transport open - closing');
  148. close();
  149. } else {
  150. // in case we're trying to close while
  151. // handshaking is in progress (GH-164)
  152. debug('transport not open - deferring close');
  153. this.once('open', close);
  154. }
  155. };
  156. /**
  157. * Writes a packets payload.
  158. *
  159. * @param {Array} data packets
  160. * @param {Function} drain callback
  161. * @api private
  162. */
  163. Polling.prototype.write = function(packets){
  164. var self = this;
  165. this.writable = false;
  166. var callbackfn = function() {
  167. self.writable = true;
  168. self.emit('drain');
  169. };
  170. var self = this;
  171. parser.encodePayload(packets, this.supportsBinary, function(data) {
  172. self.doWrite(data, callbackfn);
  173. });
  174. };
  175. /**
  176. * Generates uri for connection.
  177. *
  178. * @api private
  179. */
  180. Polling.prototype.uri = function(){
  181. var query = this.query || {};
  182. var schema = this.secure ? 'https' : 'http';
  183. var port = '';
  184. // cache busting is forced
  185. if (false !== this.timestampRequests) {
  186. query[this.timestampParam] = yeast();
  187. }
  188. if (!this.supportsBinary && !query.sid) {
  189. query.b64 = 1;
  190. }
  191. query = parseqs.encode(query);
  192. // avoid port if default for schema
  193. if (this.port && (('https' == schema && this.port != 443) ||
  194. ('http' == schema && this.port != 80))) {
  195. port = ':' + this.port;
  196. }
  197. // prepend ? to query
  198. if (query.length) {
  199. query = '?' + query;
  200. }
  201. var ipv6 = this.hostname.indexOf(':') !== -1;
  202. return schema + '://' + (ipv6 ? '[' + this.hostname + ']' : this.hostname) + port + this.path + query;
  203. };