xhr-polling.js 1.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869
  1. /*!
  2. * socket.io-node
  3. * Copyright(c) 2011 LearnBoost <[email protected]>
  4. * MIT Licensed
  5. */
  6. /**
  7. * Module requirements.
  8. */
  9. var HTTPPolling = require('./http-polling');
  10. /**
  11. * Export the constructor.
  12. */
  13. exports = module.exports = XHRPolling;
  14. /**
  15. * Ajax polling transport.
  16. *
  17. * @api public
  18. */
  19. function XHRPolling (mng, data, req) {
  20. HTTPPolling.call(this, mng, data, req);
  21. };
  22. /**
  23. * Inherits from Transport.
  24. */
  25. XHRPolling.prototype.__proto__ = HTTPPolling.prototype;
  26. /**
  27. * Transport name
  28. *
  29. * @api public
  30. */
  31. XHRPolling.prototype.name = 'xhr-polling';
  32. /**
  33. * Frames data prior to write.
  34. *
  35. * @api private
  36. */
  37. XHRPolling.prototype.doWrite = function (data) {
  38. HTTPPolling.prototype.doWrite.call(this);
  39. var origin = this.req.headers.origin
  40. , headers = {
  41. 'Content-Type': 'text/plain; charset=UTF-8'
  42. , 'Content-Length': data === undefined ? 0 : Buffer.byteLength(data)
  43. , 'Connection': 'Keep-Alive'
  44. };
  45. if (origin) {
  46. // https://developer.mozilla.org/En/HTTP_Access_Control
  47. headers['Access-Control-Allow-Origin'] = origin;
  48. headers['Access-Control-Allow-Credentials'] = 'true';
  49. }
  50. this.response.writeHead(200, headers);
  51. this.response.write(data);
  52. this.log.debug(this.name + ' writing', data);
  53. };