当前位置:   article > 正文

[CocosCreator]封装XMLHttpRequest短连接_cocos xmlhttprequest

cocos xmlhttprequest

       欢迎喜欢或者从事CocosCreator开发的小伙伴请加入我的大家庭CocosCreator游戏开发Q群:26855530 

  1. import SysLog from "./SysLog";
  2. import PublicUtil from "./PublicUtil";
  3. import ServerConfig from "../common/ServerConfig";
  4. import UserData from "../data/UserData";
  5. class HttpUtil {
  6. private static instance: HttpUtil;
  7. private constructor() {
  8. }
  9. static getInstance(): HttpUtil {
  10. if (!HttpUtil.instance) {
  11. HttpUtil.instance = new HttpUtil();
  12. }
  13. return this.instance;
  14. }
  15. httpGets(url, callback, errorCallback?) {
  16. let xhr = new XMLHttpRequest();
  17. xhr.onreadystatechange = function () {
  18. if (xhr.readyState == 4 && (xhr.status >= 200 && xhr.status < 400)) {
  19. let responseText = PublicUtil.unCode(xhr.responseText);//解密
  20. //let responseText = xhr.responseText;//SysLog("josn:"+responseText);
  21. if (xhr.status == 200) {
  22. if (responseText) {
  23. SysLog.debug("responseText:" + responseText);
  24. let jsonObj = JSON.parse(responseText);
  25. let detail = jsonObj["detail"];
  26. if (detail) {
  27. callback(detail);
  28. }
  29. }
  30. } else {
  31. SysLog.debug("连接服务器失败....");
  32. }
  33. }
  34. };
  35. xhr.onerror = function () {
  36. if (typeof errorCallback == 'function') {
  37. errorCallback();
  38. }
  39. };
  40. xhr.open("GET", url, true);
  41. if (cc.sys.isNative) {
  42. xhr.setRequestHeader("Accept-Encoding", "gzip,deflate");
  43. }
  44. // note: In Internet Explorer, the timeout property may be set only after calling the open()
  45. // method and before calling the send() method.
  46. xhr.timeout = 5000;// 5 seconds for timeout
  47. xhr.send();
  48. }
  49. gameHttpPost(url: string, params: string | any, callback, errorCallback?) {
  50. //追加session验证
  51. if (typeof params === 'string') {
  52. let sess = UserData.Map_UserInfo_ALL.get("sess");
  53. params = params.replace("}", ',"tokenId":"' + sess + '"}');
  54. }
  55. let xhr = new XMLHttpRequest();
  56. xhr.onreadystatechange = function () {
  57. if (xhr.readyState == 4 && (xhr.status >= 200 && xhr.status < 400)) {
  58. let responseText = PublicUtil.unCode(xhr.responseText);//解密
  59. //let responseText = xhr.responseText;//SysLog("josn:"+responseText);
  60. if (xhr.status == 200) {
  61. if (responseText) {
  62. //SysLog.debug("responseText:" + responseText);
  63. let jsonObj = JSON.parse(responseText);
  64. let detail = jsonObj["detail"];
  65. if (detail) {
  66. callback(detail);
  67. }
  68. }
  69. } else {
  70. if (typeof errorCallback == 'function') {
  71. errorCallback();
  72. }
  73. }
  74. }
  75. };
  76. xhr.onerror = function () {
  77. if (typeof errorCallback == 'function') {
  78. errorCallback();
  79. }
  80. };
  81. xhr.open("POST", ServerConfig.game_mainPoint + url);
  82. xhr.setRequestHeader("Content-Type", "text/plain;charset=UTF-8");
  83. xhr.timeout = 5000;// 5 seconds for timeout
  84. xhr.send(PublicUtil.enCode(params));//加密
  85. //xhr.send(params);
  86. }
  87. }
  88. export default HttpUtil;

使用方式:(简单的单例模式调用)

  1. HttpUtil.getInstance().gameHttpPost('地址', JSON.stringify(jsonObj), (datas:any) => {
  2. //后端返回的datas
  3. });

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/我家小花儿/article/detail/152644
推荐阅读
相关标签
  

闽ICP备14008679号