ICode9

精准搜索请尝试: 精确搜索
首页 > 其他分享> 文章详细

[CocosCreator]封装XMLHttpRequest短连接

2021-08-07 19:02:34  阅读:289  来源: 互联网

标签:responseText xhr detail 封装 CocosCreator let XMLHttpRequest HttpUtil errorCallbac


import SysLog from "./SysLog";
import PublicUtil from "./PublicUtil";
import ServerConfig from "../common/ServerConfig";
import UserData from "../data/UserData";

class HttpUtil {
    private static instance: HttpUtil;

    private constructor() {
    }

    static getInstance(): HttpUtil {
        if (!HttpUtil.instance) {
            HttpUtil.instance = new HttpUtil();
        }
        return this.instance;
    }

    httpGets(url, callback, errorCallback?) {
        let xhr = new XMLHttpRequest();
        xhr.onreadystatechange = function () {
            if (xhr.readyState == 4 && (xhr.status >= 200 && xhr.status < 400)) {
                let responseText = PublicUtil.unCode(xhr.responseText);//解密
                //let responseText = xhr.responseText;//SysLog("josn:"+responseText);
                if (xhr.status == 200) {
                    if (responseText) {
                        SysLog.debug("responseText:" + responseText);
                        let jsonObj = JSON.parse(responseText);
                        let detail = jsonObj["detail"];
                        if (detail) {
                            callback(detail);
                        }
                    }
                } else {
                    SysLog.debug("连接服务器失败....");
                }
            }
        };
        xhr.onerror = function () {
            if (typeof errorCallback == 'function') {
                errorCallback();
            }
        };
        xhr.open("GET", url, true);
        if (cc.sys.isNative) {
            xhr.setRequestHeader("Accept-Encoding", "gzip,deflate");
        }

        // note: In Internet Explorer, the timeout property may be set only after calling the open()
        // method and before calling the send() method.
        xhr.timeout = 5000;// 5 seconds for timeout

        xhr.send();
    }

    
    gameHttpPost(url: string, params: string | any, callback, errorCallback?) {
        //追加session验证
        if (typeof params === 'string') {
            let sess = UserData.Map_UserInfo_ALL.get("sess");
            params = params.replace("}", ',"tokenId":"' + sess + '"}');
        }

        let xhr = new XMLHttpRequest();
        xhr.onreadystatechange = function () {
            if (xhr.readyState == 4 && (xhr.status >= 200 && xhr.status < 400)) {
                let responseText = PublicUtil.unCode(xhr.responseText);//解密
                //let responseText = xhr.responseText;//SysLog("josn:"+responseText);
                if (xhr.status == 200) {
                    if (responseText) {
                        //SysLog.debug("responseText:" + responseText);
                        let jsonObj = JSON.parse(responseText);
                        let detail = jsonObj["detail"];
                        if (detail) {
                            callback(detail);
                        }
                    }
                } else {
                    if (typeof errorCallback == 'function') {
                        errorCallback();
                    }
                }
            }
        };
        xhr.onerror = function () {
            if (typeof errorCallback == 'function') {
                errorCallback();
            }
        };
        xhr.open("POST", ServerConfig.game_mainPoint + url);
        xhr.setRequestHeader("Content-Type", "text/plain;charset=UTF-8");
        xhr.timeout = 5000;// 5 seconds for timeout
        xhr.send(PublicUtil.enCode(params));//加密
        //xhr.send(params);
    }

    
}

export default HttpUtil;

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

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

标签:responseText,xhr,detail,封装,CocosCreator,let,XMLHttpRequest,HttpUtil,errorCallbac
来源: https://blog.csdn.net/qq183293/article/details/119490986

本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享;
2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关;
3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关;
4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除;
5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。

专注分享技术,共同学习,共同进步。侵权联系[81616952@qq.com]

Copyright (C)ICode9.com, All Rights Reserved.

ICode9版权所有