PageRenderTime 4ms CodeModel.GetById 0ms RepoModel.GetById 1ms app.codeStats 0ms

/js/lib/Socket.IO-node/lib/socket.io/transports/htmlfile.js

http://github.com/onedayitwillmake/RealtimeMultiplayerNodeJs
JavaScript | 84 lines | 42 code | 15 blank | 27 comment | 2 complexity | 152d8689ecddb0a9fb90e8bbdf0dd4b3 MD5 | raw file
Possible License(s): GPL-2.0, LGPL-2.1, MPL-2.0-no-copyleft-exception, BSD-3-Clause
  1. /*!
  2. * Socket.IO - transports - HTMLFile
  3. * Copyright (c) 2010-2011 Guillermo Rauch <guillermo@learnboost.com>
  4. * MIT Licensed
  5. */
  6. var Client = require('../client')
  7. , qs = require('querystring');
  8. /**
  9. * Expose `HTMLFile`.
  10. */
  11. module.exports = HTMLFile;
  12. /**
  13. * Initialize a `HTMLFile` client.
  14. *
  15. * @api private
  16. */
  17. function HTMLFile() {
  18. Client.apply(this, arguments);
  19. };
  20. /**
  21. * Inherit from `Client.prototype`.
  22. */
  23. HTMLFile.prototype.__proto__ = Client.prototype;
  24. /**
  25. * Connection implementation.
  26. *
  27. * @api private
  28. */
  29. HTMLFile.prototype._onConnect = function(req, res){
  30. var self = this
  31. , body = '';
  32. switch (req.method){
  33. case 'GET':
  34. Client.prototype._onConnect.call(this, req, res);
  35. res.useChunkedEncodingByDefault = true;
  36. res.shouldKeepAlive = true;
  37. res.setHeader('Content-Type', 'text/html');
  38. res.setHeader('Connection', 'keep-alive');
  39. res.setHeader('Transfer-Encoding', 'chunked');
  40. res.write('<html><body>' + new Array(245).join(' '));
  41. this._payload();
  42. break;
  43. case 'POST':
  44. req.on('data', function(message){ body += message; });
  45. req.on('end', function(){
  46. try {
  47. var msg = qs.parse(body);
  48. self._onMessage(msg.data);
  49. } catch(e){
  50. self.listener.log('htmlfile message handler error - ' + e.stack);
  51. }
  52. res.setHeader('Content-Type', 'text/plain');
  53. res.end('ok');
  54. });
  55. break;
  56. }
  57. };
  58. /**
  59. * Write implementation.
  60. *
  61. * @param {String} message
  62. * @api private
  63. */
  64. HTMLFile.prototype._write = function(message){
  65. if (this._open) {
  66. message = JSON.stringify(message);
  67. this.response.write('<script>parent.s._(' + message + ', document);</script>');
  68. }
  69. };