WebDataCenter.js 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131
  1. WebDataCenter = cc.Class({
  2. ctor:function () {
  3. this.m_LinkCount = 0;
  4. this.m_WaitLinkArr = new Array();
  5. this.m_DataMap = new Object();
  6. },
  7. //保存数据结构
  8. GetDataObj:function(Url, NewData) {
  9. if( this.m_DataMap[Url] == null) this.m_DataMap[Url] = new Object();
  10. this.m_DataMap[Url].Time = new Date().getTime();
  11. this.m_DataMap[Url].Data = NewData;
  12. },
  13. SetDataOutTime:function(KeyWord) {
  14. for(var i in this.m_DataMap){
  15. if( i.indexOf(KeyWord) >= 0 ) this.m_DataMap[i].Time = 0;
  16. }
  17. },
  18. //OutTime :null无需保存数据
  19. GetData:function(WebUrl, OutTime, CallBack) {
  20. var Now = new Date().getTime();
  21. //不保存,无旧数据,或旧数据过期 需要重新加载
  22. if(OutTime == null || this.m_DataMap[WebUrl] == null || this.m_DataMap[WebUrl].Time + OutTime*1000 < Now){
  23. //正在连接达到上限
  24. if( this.m_LinkCount >= 10) this.m_WaitLinkArr.push([WebUrl,OutTime,CallBack]);
  25. else this.LinkWeb([WebUrl, OutTime, CallBack]);
  26. }else{
  27. //已有数据
  28. if(window.LOG_WEB_DATA)console.log("HttpLink "+WebUrl)
  29. if(window.LOG_WEB_DATA)console.log("HttpReq "+this.m_DataMap[WebUrl].Data)
  30. CallBack(this.m_DataMap[WebUrl].Data);
  31. }
  32. },
  33. LinkWeb:function (Arr) {
  34. try {
  35. this.httpGets(Arr[0], function (data) {
  36. //保存数据
  37. if(Arr[1] != null) this.GetDataObj(Arr[0], data);
  38. //完成回调
  39. Arr[2](data);
  40. //队列执行
  41. if(this.m_WaitLinkArr.length > 0) this.LinkWeb(this.m_WaitLinkArr.shift());
  42. }.bind(this));
  43. } catch (error) {
  44. if(this.m_WaitLinkArr.length > 0) this.LinkWeb(this.m_WaitLinkArr.shift());
  45. }
  46. },
  47. httpGets:function (Url, CallBack) {
  48. this.m_LinkCount++;
  49. var xhr = new XMLHttpRequest();;
  50. xhr.onreadystatechange = function () {
  51. if (xhr.readyState === 4 && (xhr.status >= 200 && xhr.status < 400)) {
  52. this.m_LinkCount--;
  53. var respone = decodeURI(xhr.responseText);
  54. if(window.LOG_WEB_DATA)console.log("HttpLink "+Url);
  55. respone = respone.replace(/\s+\r\n/g,'');
  56. while(respone != '' && respone[0].charCodeAt() == 65279){//口或?开头 原因不明
  57. var end1 = respone.lastIndexOf("}");
  58. var end2 = respone.lastIndexOf("]");
  59. var end = Math.max(end1, end2)
  60. end = end>=0?end+1:respone.length;
  61. respone = respone.substring(1, end );
  62. }
  63. if(window.LOG_WEB_DATA)console.log("HttpReq "+respone)
  64. CallBack(respone);
  65. }
  66. }.bind(this);
  67. // if (cc.sys.isNative) {
  68. // xhr.setRequestHeader("Accept-Encoding", "gzip,deflate");
  69. // }
  70. xhr.timeout = 5000;// 5 seconds for timeout
  71. xhr.open("GET", encodeURI(Url), true);
  72. xhr.send();
  73. },
  74. httpPosts:function (Url, CallBack) {
  75. this.m_LinkCount++;
  76. var xhr = new XMLHttpRequest();
  77. xhr.onreadystatechange = function () {
  78. if (xhr.readyState === 4 && (xhr.status >= 200 && xhr.status < 400)) {
  79. this.m_LinkCount--;
  80. var respone = xhr.responseText;
  81. if(window.LOG_WEB_DATA)console.log("HttpLink "+Url)
  82. while(respone != '' && respone[0].charCodeAt() == 65279){//口或?开头 原因不明
  83. var end1 = respone.lastIndexOf("}");
  84. var end2 = respone.lastIndexOf("]");
  85. var end = Math.max(end1, end2)
  86. end = end>=0?end+1:respone.length;
  87. respone = respone.substring(1, end );
  88. }
  89. if(window.LOG_WEB_DATA)console.log("HttpReq "+respone)
  90. CallBack(respone);
  91. }
  92. }.bind(this);
  93. if (cc.sys.isNative) {
  94. xhr.setRequestHeader("Accept-Encoding", "gzip,deflate");
  95. }
  96. xhr.timeout = 5000;// 5 seconds for timeout
  97. xhr.open("POST", Url, true);
  98. xhr.send();
  99. },
  100. httpPOST(url, data, callback) {
  101. let dataStr = '';
  102. Object.keys(data).forEach(key => {
  103. dataStr += key + '=' + encodeURIComponent(data[key]) + '&';
  104. })
  105. if (dataStr !== '') {
  106. dataStr = dataStr.substring(0, dataStr.lastIndexOf('&'));
  107. }
  108. var xhr = new XMLHttpRequest();
  109. xhr.open("POST", url, true);
  110. xhr.setRequestHeader("Content-Type","application/x-www-form-urlencoded;charset=UTF-8");
  111. xhr.onreadystatechange = function () {
  112. if (xhr.readyState === 4 && (xhr.status >= 200 && xhr.status < 300)) {
  113. let response = xhr.responseText.replace(/\s+/g, '');
  114. console.log("post data return responseText: ", response);
  115. callback(JSON.parse(response));
  116. }
  117. };
  118. xhr.send(dataStr);
  119. },
  120. });
  121. WebCenter = new WebDataCenter();