如何将protobuf base64字符串解码为js对象
How to decode protobuf base64 string to js object
我正在尝试将来自服务器端(base64 字符串)的 protobuf 消息解码为 javascript 对象。用于解码 protobuf.js.
正如医生建议的那样:
var builder = ProtoBuf.newBuilder({ convertFieldsToCamelCase: true });
var YourMessage = builder.build("YourMessage");
var buffer = ...; // E.g. a buffer received on a WebSocket
var myMessage = YourMessage.decode(buffer);
...
var b64str = ...; // E.g. a string fetched via AJAX
var myMessage = YourMessage.decode64(b64str);
我就是这样做的(数据 - base64 字符串):
var proto = dcodeIO.ProtoBuf;
var buffer = dcodeIO.ByteBuffer;
var b = buffer.wrap(data,"binary");
var builder = proto.newBuilder({ convertFieldsToCamelCase: true });
builder.define("Events");
var message = builder.build("Events");
var result = message.decode(b); //also try to decode base64 string - message.decode64(data);
我收到错误
decode/decode64 undefined
缺少导入原型定义的行,如下所示(其中 tests/example1.proto 是您的文件名)
ProtoBuf.loadProtoFile("tests/example1.proto", builder);
或者如果从原始字符串加载
ProtoBuf.loadProto(...protoString..., "example1.proto");
如果没有这个,构建器会说未定义,因为它没有任何东西可以用
构建它的定义
我正在尝试将来自服务器端(base64 字符串)的 protobuf 消息解码为 javascript 对象。用于解码 protobuf.js.
正如医生建议的那样:
var builder = ProtoBuf.newBuilder({ convertFieldsToCamelCase: true });
var YourMessage = builder.build("YourMessage");
var buffer = ...; // E.g. a buffer received on a WebSocket
var myMessage = YourMessage.decode(buffer);
...
var b64str = ...; // E.g. a string fetched via AJAX
var myMessage = YourMessage.decode64(b64str);
我就是这样做的(数据 - base64 字符串):
var proto = dcodeIO.ProtoBuf;
var buffer = dcodeIO.ByteBuffer;
var b = buffer.wrap(data,"binary");
var builder = proto.newBuilder({ convertFieldsToCamelCase: true });
builder.define("Events");
var message = builder.build("Events");
var result = message.decode(b); //also try to decode base64 string - message.decode64(data);
我收到错误
decode/decode64 undefined
缺少导入原型定义的行,如下所示(其中 tests/example1.proto 是您的文件名)
ProtoBuf.loadProtoFile("tests/example1.proto", builder);
或者如果从原始字符串加载
ProtoBuf.loadProto(...protoString..., "example1.proto");
如果没有这个,构建器会说未定义,因为它没有任何东西可以用
构建它的定义