htmlfile.js 1.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182
  1. /*!
  2. * socket.io-node
  3. * Copyright(c) 2011 LearnBoost <dev@learnboost.com>
  4. * MIT Licensed
  5. */
  6. /**
  7. * Module requirements.
  8. */
  9. var HTTPTransport = require('./http');
  10. /**
  11. * Export the constructor.
  12. */
  13. exports = module.exports = HTMLFile;
  14. /**
  15. * HTMLFile transport constructor.
  16. *
  17. * @api public
  18. */
  19. function HTMLFile (mng, data, req) {
  20. HTTPTransport.call(this, mng, data, req);
  21. };
  22. /**
  23. * Inherits from Transport.
  24. */
  25. HTMLFile.prototype.__proto__ = HTTPTransport.prototype;
  26. /**
  27. * Transport name
  28. *
  29. * @api public
  30. */
  31. HTMLFile.prototype.name = 'htmlfile';
  32. /**
  33. * Handles the request.
  34. *
  35. * @api private
  36. */
  37. HTMLFile.prototype.handleRequest = function (req) {
  38. HTTPTransport.prototype.handleRequest.call(this, req);
  39. if (req.method == 'GET') {
  40. req.res.writeHead(200, {
  41. 'Content-Type': 'text/html; charset=UTF-8'
  42. , 'Connection': 'keep-alive'
  43. , 'Transfer-Encoding': 'chunked'
  44. });
  45. req.res.write(
  46. '<html><body>'
  47. + '<script>var _ = function (msg) { parent.s._(msg, document); };</script>'
  48. + new Array(174).join(' ')
  49. );
  50. }
  51. };
  52. /**
  53. * Performs the write.
  54. *
  55. * @api private
  56. */
  57. HTMLFile.prototype.write = function (data) {
  58. data = '<script>_(' + JSON.stringify(data) + ');</script>';
  59. if (this.response.write(data)) {
  60. this.drained = true;
  61. }
  62. this.log.debug(this.name + ' writing', data);
  63. };