PageRenderTime 23ms CodeModel.GetById 12ms RepoModel.GetById 0ms app.codeStats 0ms

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

http://github.com/onedayitwillmake/RealtimeMultiplayerNodeJs
JavaScript | 120 lines | 91 code | 21 blank | 8 comment | 7 complexity | 15d1bf9412804f2f2b6777ec0f2bc95f 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 client
  3. *
  4. * @author Guillermo Rauch <guillermo@learnboost.com>
  5. * @license The MIT license.
  6. * @copyright Copyright (c) 2010 LearnBoost <dev@learnboost.com>
  7. */
  8. (function(){
  9. var io = this.io;
  10. io.JSONP = [];
  11. JSONPPolling = io.Transport['jsonp-polling'] = function(){
  12. io.Transport.XHR.apply(this, arguments);
  13. this._insertAt = document.getElementsByTagName('script')[0];
  14. this._index = io.JSONP.length;
  15. io.JSONP.push(this);
  16. };
  17. io.util.inherit(JSONPPolling, io.Transport['xhr-polling']);
  18. JSONPPolling.prototype.type = 'jsonp-polling';
  19. JSONPPolling.prototype._send = function(data){
  20. var self = this;
  21. if (!('_form' in this)){
  22. var form = document.createElement('FORM'),
  23. area = document.createElement('TEXTAREA'),
  24. id = this._iframeId = 'socket_io_iframe_' + this._index,
  25. iframe;
  26. form.style.position = 'absolute';
  27. form.style.top = '-1000px';
  28. form.style.left = '-1000px';
  29. form.target = id;
  30. form.method = 'POST';
  31. form.action = this._prepareUrl() + '/' + (+new Date) + '/' + this._index;
  32. area.name = 'data';
  33. form.appendChild(area);
  34. this._insertAt.parentNode.insertBefore(form, this._insertAt);
  35. document.body.appendChild(form);
  36. this._form = form;
  37. this._area = area;
  38. }
  39. function complete(){
  40. initIframe();
  41. self._posting = false;
  42. self._checkSend();
  43. };
  44. function initIframe(){
  45. if (self._iframe){
  46. self._form.removeChild(self._iframe);
  47. }
  48. try {
  49. // ie6 dynamic iframes with target="" support (thanks Chris Lambacher)
  50. iframe = document.createElement('<iframe name="'+ self._iframeId +'">');
  51. } catch(e){
  52. iframe = document.createElement('iframe');
  53. iframe.name = self._iframeId;
  54. }
  55. iframe.id = self._iframeId;
  56. self._form.appendChild(iframe);
  57. self._iframe = iframe;
  58. };
  59. initIframe();
  60. this._posting = true;
  61. this._area.value = data;
  62. try {
  63. this._form.submit();
  64. } catch(e){}
  65. if (this._iframe.attachEvent){
  66. iframe.onreadystatechange = function(){
  67. if (self._iframe.readyState == 'complete') complete();
  68. };
  69. } else {
  70. this._iframe.onload = complete;
  71. }
  72. };
  73. JSONPPolling.prototype._get = function(){
  74. var self = this,
  75. script = document.createElement('SCRIPT');
  76. if (this._script){
  77. this._script.parentNode.removeChild(this._script);
  78. this._script = null;
  79. }
  80. script.async = true;
  81. script.src = this._prepareUrl() + '/' + (+new Date) + '/' + this._index;
  82. script.onerror = function(){
  83. self._onDisconnect();
  84. };
  85. this._insertAt.parentNode.insertBefore(script, this._insertAt);
  86. this._script = script;
  87. };
  88. JSONPPolling.prototype._ = function(){
  89. this._onData.apply(this, arguments);
  90. this._get();
  91. return this;
  92. };
  93. JSONPPolling.check = function(){
  94. return true;
  95. };
  96. JSONPPolling.xdomainCheck = function(){
  97. return true;
  98. };
  99. })();