http-polling.js 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147
  1. /*!
  2. * socket.io-node
  3. * Copyright(c) 2011 LearnBoost <[email protected]>
  4. * MIT Licensed
  5. */
  6. /**
  7. * Module requirements.
  8. */
  9. var HTTPTransport = require('./http');
  10. /**
  11. * Exports the constructor.
  12. */
  13. exports = module.exports = HTTPPolling;
  14. /**
  15. * HTTP polling constructor.
  16. *
  17. * @api public.
  18. */
  19. function HTTPPolling (mng, data, req) {
  20. HTTPTransport.call(this, mng, data, req);
  21. };
  22. /**
  23. * Inherits from HTTPTransport.
  24. *
  25. * @api public.
  26. */
  27. HTTPPolling.prototype.__proto__ = HTTPTransport.prototype;
  28. /**
  29. * Transport name
  30. *
  31. * @api public
  32. */
  33. HTTPPolling.prototype.name = 'httppolling';
  34. /**
  35. * Override setHandlers
  36. *
  37. * @api private
  38. */
  39. HTTPPolling.prototype.setHandlers = function () {
  40. HTTPTransport.prototype.setHandlers.call(this);
  41. this.socket.removeListener('end', this.bound.end);
  42. this.socket.removeListener('close', this.bound.close);
  43. };
  44. /**
  45. * Removes heartbeat timeouts for polling.
  46. */
  47. HTTPPolling.prototype.setHeartbeatInterval = function () {
  48. return this;
  49. };
  50. /**
  51. * Handles a request
  52. *
  53. * @api private
  54. */
  55. HTTPPolling.prototype.handleRequest = function (req) {
  56. HTTPTransport.prototype.handleRequest.call(this, req);
  57. if (req.method == 'GET') {
  58. var self = this;
  59. this.pollTimeout = setTimeout(function () {
  60. self.packet({ type: 'noop' });
  61. self.log.debug(self.name + ' closed due to exceeded duration');
  62. }, this.manager.get('polling duration') * 1000);
  63. this.log.debug('setting poll timeout');
  64. }
  65. };
  66. /**
  67. * Clears polling timeout
  68. *
  69. * @api private
  70. */
  71. HTTPPolling.prototype.clearPollTimeout = function () {
  72. if (this.pollTimeout) {
  73. clearTimeout(this.pollTimeout);
  74. this.pollTimeout = null;
  75. this.log.debug('clearing poll timeout');
  76. }
  77. return this;
  78. };
  79. /**
  80. * Override clear timeouts to clear the poll timeout
  81. *
  82. * @api private
  83. */
  84. HTTPPolling.prototype.clearTimeouts = function () {
  85. HTTPTransport.prototype.clearTimeouts.call(this);
  86. this.clearPollTimeout();
  87. };
  88. /**
  89. * doWrite to clear poll timeout
  90. *
  91. * @api private
  92. */
  93. HTTPPolling.prototype.doWrite = function () {
  94. this.clearPollTimeout();
  95. };
  96. /**
  97. * Performs a write.
  98. *
  99. * @api private.
  100. */
  101. HTTPPolling.prototype.write = function (data, close) {
  102. this.doWrite(data);
  103. this.response.end();
  104. this.onClose();
  105. };
  106. /**
  107. * Override end.
  108. *
  109. * @api private
  110. */
  111. HTTPPolling.prototype.end = function (reason) {
  112. this.clearPollTimeout();
  113. return HTTPTransport.prototype.end.call(this, reason);
  114. };