PageRenderTime 32ms CodeModel.GetById 28ms RepoModel.GetById 0ms app.codeStats 0ms

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

http://github.com/onedayitwillmake/RealtimeMultiplayerNodeJs
JavaScript | 77 lines | 31 code | 16 blank | 30 comment | 4 complexity | 137d74e4247636344f6dca13be17abe4 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 - JSONPPolling
  3. * Copyright (c) 2010-2011 Guillermo Rauch <guillermo@learnboost.com>
  4. * MIT Licensed
  5. */
  6. var XHRPolling = require('./xhr-polling');
  7. /**
  8. * Expose `JSONPPolling`.
  9. */
  10. module.exports = JSONPPolling;
  11. /**
  12. * Initialize a `JSONPPolling` client.
  13. *
  14. * @api private
  15. */
  16. function JSONPPolling() {
  17. XHRPolling.apply(this, arguments);
  18. };
  19. /**
  20. * Inherit from `XHRPolling.prototype`.
  21. */
  22. JSONPPolling.prototype.__proto__ = XHRPolling.prototype;
  23. /**
  24. * Options.
  25. */
  26. JSONPPolling.prototype.options = {
  27. timeout: null
  28. , closeTimeout: 8000
  29. , duration: 20000
  30. };
  31. /**
  32. * Connection implementation.
  33. *
  34. * @api private
  35. */
  36. JSONPPolling.prototype._onConnect = function(req, res){
  37. this._index = req.url.match(/\/([0-9]+)\/?$/).pop();
  38. XHRPolling.prototype._onConnect.call(this, req, res);
  39. };
  40. /**
  41. * Write implementation.
  42. *
  43. * @param {String} message
  44. * @api private
  45. */
  46. JSONPPolling.prototype._write = function(message){
  47. if (this._open){
  48. var req = this.request
  49. , res = this.response
  50. , origin = req.headers.origin;
  51. if (origin && !this._verifyOrigin(origin)){
  52. message = "alert('Cross domain security restrictions not met');";
  53. } else {
  54. message = "io.JSONP["+ this._index +"]._("+ JSON.stringify(message) +");";
  55. }
  56. res.setHeader('Content-Type', 'text/javascript; charset=UTF-8');
  57. res.setHeader('Content-Length', Buffer.byteLength(message));
  58. res.end(message);
  59. this._onClose();
  60. }
  61. };