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

http://github.com/onedayitwillmake/RealtimeMultiplayerNodeJs · JavaScript · 136 lines · 109 code · 18 blank · 9 comment · 25 complexity · 5ba9a90f5995d37664a7e433eafb0130 MD5 · raw file

  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. var empty = new Function,
  11. XMLHttpRequestCORS = (function(){
  12. if (!('XMLHttpRequest' in window)) return false;
  13. // CORS feature detection
  14. var a = new XMLHttpRequest();
  15. return a.withCredentials != undefined;
  16. })(),
  17. request = function(xdomain){
  18. if ('XDomainRequest' in window && xdomain) return new XDomainRequest();
  19. if ('XMLHttpRequest' in window && (!xdomain || XMLHttpRequestCORS)) return new XMLHttpRequest();
  20. if (!xdomain){
  21. try {
  22. var a = new ActiveXObject('MSXML2.XMLHTTP');
  23. return a;
  24. } catch(e){}
  25. try {
  26. var b = new ActiveXObject('Microsoft.XMLHTTP');
  27. return b;
  28. } catch(e){}
  29. }
  30. return false;
  31. },
  32. XHR = io.Transport.XHR = function(){
  33. io.Transport.apply(this, arguments);
  34. this._sendBuffer = [];
  35. };
  36. io.util.inherit(XHR, io.Transport);
  37. XHR.prototype.connect = function(){
  38. this._get();
  39. return this;
  40. };
  41. XHR.prototype._checkSend = function(){
  42. if (!this._posting && this._sendBuffer.length){
  43. var encoded = this._encode(this._sendBuffer);
  44. this._sendBuffer = [];
  45. this._send(encoded);
  46. }
  47. };
  48. XHR.prototype.send = function(data){
  49. if (io.util.isArray(data)){
  50. this._sendBuffer.push.apply(this._sendBuffer, data);
  51. } else {
  52. this._sendBuffer.push(data);
  53. }
  54. this._checkSend();
  55. return this;
  56. };
  57. XHR.prototype._send = function(data){
  58. var self = this;
  59. this._posting = true;
  60. this._sendXhr = this._request('send', 'POST');
  61. this._sendXhr.onreadystatechange = function(){
  62. var status;
  63. if (self._sendXhr.readyState == 4){
  64. self._sendXhr.onreadystatechange = empty;
  65. try { status = self._sendXhr.status; } catch(e){}
  66. self._posting = false;
  67. if (status == 200){
  68. self._checkSend();
  69. } else {
  70. self._onDisconnect();
  71. }
  72. }
  73. };
  74. this._sendXhr.send('data=' + encodeURIComponent(data));
  75. };
  76. XHR.prototype.disconnect = function(){
  77. // send disconnection signal
  78. this._onDisconnect();
  79. return this;
  80. };
  81. XHR.prototype._onDisconnect = function(){
  82. if (this._xhr){
  83. this._xhr.onreadystatechange = empty;
  84. try {
  85. this._xhr.abort();
  86. } catch(e){}
  87. this._xhr = null;
  88. }
  89. if (this._sendXhr){
  90. this._sendXhr.onreadystatechange = empty;
  91. try {
  92. this._sendXhr.abort();
  93. } catch(e){}
  94. this._sendXhr = null;
  95. }
  96. this._sendBuffer = [];
  97. io.Transport.prototype._onDisconnect.call(this);
  98. };
  99. XHR.prototype._request = function(url, method, multipart){
  100. var req = request(this.base._isXDomain());
  101. if (multipart) req.multipart = true;
  102. req.open(method || 'GET', this._prepareUrl() + (url ? '/' + url : ''));
  103. if (method == 'POST' && 'setRequestHeader' in req){
  104. req.setRequestHeader('Content-type', 'application/x-www-form-urlencoded; charset=utf-8');
  105. }
  106. return req;
  107. };
  108. XHR.check = function(xdomain){
  109. try {
  110. if (request(xdomain)) return true;
  111. } catch(e){}
  112. return false;
  113. };
  114. XHR.xdomainCheck = function(){
  115. return XHR.check(true);
  116. };
  117. XHR.request = request;
  118. })();