当前位置 : 主页 > 手机开发 > 其它 >

Cocos-JS 网络通讯读写消息以及加载proto文件

来源:互联网 收集:自由互联 发布时间:2021-06-13
/** * Created by Lovell on 16/6/20. */ /** * html decode * @param str * @returns {string} */ pg. htmlDecodeByRegExp = function (str) { var s = "" ; if (str. length == 0 ) return "" ; s = str. replace ( /amp;/g , "" ) ; s = s. replace ( /lt;
/**
 * Created by Lovell on 16/6/20.
 */

/**
 * html decode
 * @param str
 * @returns {string}
 */
pg.htmlDecodeByRegExp = function (str) {
    var s = "";
    if (str.length == 0) return "";
    s = str.replace(/&/g, "&");
    s = s.replace(/&lt;/g, "<");
    s = s.replace(/&gt;/g, ">");
    s = s.replace(/&nbsp;/g, " ");
    s = s.replace(/&#39;/g, "\'");
    s = s.replace(/&quot;&quot;&quot;/g, "\"");
    return s;
};

/**
 * safe retain
 * @param obj
 * @constructor
 */
pg.SAFERETAIN = function (obj) {
    if (obj && obj.retain) {
        obj.retain();
    }
};

/**
 * safe release
 * @param obj
 * @constructor
 */
pg.SAFERELEASE = function (obj) {
    if (obj && obj.release) {
        obj.release();
    }
};

/**
 * load proto file, return contents
 * @param path
 * @param str
 * @returns {!ProtoBuf.Builder.Message|!Object.<string, *>}
 */
pg.loadProto = function (path, str) {
    if (typeof dcodeIO === 'undefined' || !dcodeIO.ProtoBuf) {
        throw (new Error("ProtoBuf.js is not present. Plese see Please see www/index.html for manual setup instructions."))
    }
    var ProtoBuf = dcodeIO.ProtoBuf;
    var Contents = ProtoBuf.loadProtoFile(path).build(str);
    return Contents ? Contents : null;
};

/**
 * create proto
 * @param path
 * @param str
 * @returns {*}
 */
pg.createProto = function (path, str) {
    var Proto = new pg.loadProto(path, str);
    var data = new Proto();
    return data ? data : null;
};

/**
 * read msg
 * @param msg
 * @returns {Uint8Array}
 */
pg.readMsg = function (msg) {
    if (!msg) {
        return null;
    }
    var binary = new Uint8Array(msg);
    var message = new Uint8Array(binary.byteLength - 4);
    for (var i = 0; i < binary.byteLength - 4; i++) {
        message[i] = binary[4 + i];
    }
    return message;
};

/**
 * write msg
 * @param type
 * @param msg
 * @returns {Uint8Array}
 */
pg.writeMsg = function (type, msg) {
    if (!type || !msg) {
        return null;
    }
    var binary = msg.toArrayBuffer();
    var byteLength = binary.byteLength + 4;
    msg = new ArrayBuffer(4 + byteLength);
    var view = new Uint8Array(msg);
    var len = 2;
    var tmp = new Uint8Array(len);
    for (var i = 0; i < len; i++) {
        tmp[i] = 0xff & (byteLength >> (8 * (len - i - 1)));
        view[i] = tmp[i];
    }
    tmp = new Uint8Array(len);
    for (var i = 0; i < len; i++) {
        tmp[i] = 0xff & (type >> (8 * (len - i - 1)));
        view[i + 2] = tmp[i];
    }
    tmp = new Uint8Array(binary);
    for (var i = 0; i < byteLength; i++) {
        view[i + 4] = tmp[i];
    }
    return msg;
};

/**
 * get msg type
 * @param msg
 * @returns {number}
 */
pg.getMsgType = function (msg) {
    if (!msg) {
        return 0;
    }
    var binary = new Uint8Array(msg);
    var type = 0;
    for (var i = 0; i < 2; i++) {
        type |= binary[i + 2] << (8 * (2 - i - 1));
    }
    return type;
};
网友评论