PageRenderTime 37ms CodeModel.GetById 9ms RepoModel.GetById 0ms app.codeStats 0ms

/js/lib/Socket.IO-node/lib/socket.io/transports/xhr-polling.js

http://github.com/onedayitwillmake/RealtimeMultiplayerNodeJs
JavaScript | 132 lines | 70 code | 24 blank | 38 comment | 11 complexity | 297e2bc0fbf243691772bb95013dd80b 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 - Polling
  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 `Polling`.
  10. */
  11. module.exports = Polling;
  12. /**
  13. * Initialize a `Polling` client.
  14. *
  15. * @api private
  16. */
  17. function Polling() {
  18. Client.apply(this, arguments);
  19. };
  20. /**
  21. * Inherit from `Client.prototype`.
  22. */
  23. Polling.prototype.__proto__ = Client.prototype;
  24. /**
  25. * Options.
  26. */
  27. Polling.prototype.options = {
  28. timeout: null
  29. , closeTimeout: 8000
  30. , duration: 20000
  31. };
  32. /**
  33. * Connection implementation.
  34. *
  35. * @api private
  36. */
  37. Polling.prototype._onConnect = function(req, res){
  38. var self = this
  39. , body = '';
  40. switch (req.method){
  41. case 'GET':
  42. Client.prototype._onConnect.call(this, req, res);
  43. this._closeTimeout = setTimeout(function(){
  44. self._write('');
  45. }, this.duration);
  46. this._payload();
  47. break;
  48. case 'POST':
  49. req.on('data', function(chunk){ body += chunk; });
  50. req.on('end', function(){
  51. res.setHeader('Content-Type', 'text/plain');
  52. if (req.headers.origin){
  53. if (self._verifyOrigin(req.headers.origin)){
  54. res.setHeader('Access-Control-Allow-Origin', '*');
  55. if (req.headers.cookie) {
  56. res.setHeader('Access-Control-Allow-Credentials', 'true');
  57. }
  58. } else {
  59. res.statusCode = 401;
  60. res.end('unauthorized');
  61. return;
  62. }
  63. }
  64. try {
  65. // optimization: just strip first 5 characters here?
  66. // ^ totally
  67. var msg = qs.parse(body);
  68. self._onMessage(msg.data);
  69. } catch(e){
  70. self.listener.log('xhr-polling message handler error - ' + e.stack);
  71. }
  72. res.end('ok');
  73. });
  74. break;
  75. }
  76. };
  77. /**
  78. * Close Implementation.
  79. *
  80. * @api private
  81. */
  82. Polling.prototype._onClose = function(){
  83. if (this._closeTimeout) clearTimeout(this._closeTimeout);
  84. return Client.prototype._onClose.call(this);
  85. };
  86. /**
  87. * Write implementation.
  88. *
  89. * @param {String} message
  90. * @api private
  91. */
  92. Polling.prototype._write = function(message){
  93. if (this._open) {
  94. var res = this.response
  95. , req = this.request
  96. , origin = req.headers.origin;
  97. res.setHeader('Content-Type', 'text/plain; charset=UTF-8');
  98. res.setHeader('Content-Length', Buffer.byteLength(message));
  99. // https://developer.mozilla.org/En/HTTP_Access_Control
  100. if (origin && this._verifyOrigin(origin)){
  101. origin = 'null' == origin ? '*' : origin;
  102. res.setHeader('Access-Control-Allow-Origin', origin);
  103. if (req.headers.cookie) res.setHeader('Access-Control-Allow-Credentials', 'true');
  104. }
  105. res.end(message);
  106. this._onClose();
  107. }
  108. };