关于本地文件的加载

//提供proto获取的方法
(function () {
    var ProtoMgr = {};
    ProtoMgr.protos = {};
    //1.确保proto只被加载一次
    //2.提供默认路径方便客户端调用
    ProtoMgr.get = function (protoName, buildName) {
        buildName = buildName || "external";
        var key = protoName + buildName;
        var ret = null;
        if (typeof ProtoMgr.protos[key] === 'undefined') {
            var path = 'res/raw-assets/res/model/proto/' + protoName + '.proto';
            ret = ProtoBuf.loadProtoFile(path).build(buildName);
            ProtoMgr.protos[key] = ret;
        } else {
            ret = ProtoMgr.protos[key];
        }
        return ret;
    }
    module["exports"] = ProtoMgr;
})();

以上是使用Protobuf.js中同步加载文本内容文件的方法
还有一个是加载本地化配置文件。
路径都是 res/raw-assets/res/xxx(具体文件夹名)

一旦初始化的时候使用了这样的下载方式,就会有以上报错。注释掉就没错了。

环境:模拟器运行。
浏览器运行是OK的,web版的也是准备上线了~

补充一下,我用的加载方式是 protobuf.js中的
/**
* Fetches a resource.
* @param {string} path Resource path
* @param {function(?string)=} callback Callback receiving the resource’s contents. If omitted the resource will
* be fetched synchronously. If the request failed, contents will be null.
* @return {?string|undefined} Resource contents if callback is omitted (null if the request failed), else undefined.
* @expose
*/
> Util.fetch = function(path, callback) {

            if (callback && typeof callback != 'function')
                callback = null;
            if (Util.IS_NODE) {
                var fs = require("fs");
                if (callback) {
                    fs.readFile(path, function(err, data) {
                        if (err)
                            callback(null);
                        else
                            callback(""+data);
                    });
                } else
                    try {
                        return fs.readFileSync(path);
                    } catch (e) {
                        return null;
                    }
            } else {
                var xhr = Util.XHR();
                xhr.open('GET', path, callback ? true : false);
                // xhr.setRequestHeader('User-Agent', 'XMLHTTP/1.0');
                xhr.setRequestHeader('Accept', 'text/plain');
                if (typeof xhr.overrideMimeType === 'function') xhr.overrideMimeType('text/plain');
                if (callback) {
                    xhr.onreadystatechange = function() {
                        if (xhr.readyState != 4) return;
                        if (/* remote */ xhr.status == 200 || /* local */ (xhr.status == 0 && typeof xhr.responseText === 'string'))
                            callback(xhr.responseText);
                        else
                            callback(null);
                    };
                    if (xhr.readyState == 4)
                        return;
                    xhr.send(null);
                } else {
                    xhr.send(null);
                    if (/* remote */ xhr.status == 200 || /* local */ (xhr.status == 0 && typeof xhr.responseText === 'string'))
                        return xhr.responseText;
                    return null;
                }
            }
        };

因为这个加载方式是同步的,官方提供的加载都是异步的,不能满足我的需求。

我估计有可能是你设置了 Accept,但是 overrideMimeType 在 JSB 中是空实现

xhr.setRequestHeader('Accept', 'text/plain');

所以当服务端传回数据的时候,mime type 可能是 application/protobuffer,不等同于 text/plain,导致报错 unsupported type

你可以试试不设置 Accept

我尝试了一下,将XXX换成各种都不行,直接注释掉不用也不行。
主要是我不清楚JSB的响应是什么样的。
web版可以的话,JSB不同就没法玩了。

xhr.setRequestHeader(XXX, ‘text/plain’);