zhang19910325 / protobufferforwechat Goto Github PK
View Code? Open in Web Editor NEW针对微信小程序使用的protoBuffer库
针对微信小程序使用的protoBuffer库
pbjs
Usage: pbjs [options] <schema_path>
Options:
-h, --help output usage information
-V, --version output the version number
--js <js_path> Generate JavaScript code
--decode <msg_type> Decode standard input to JSON
--encode <msg_type> Encode standard input to JSON
例如:
map<string, string> spareParameter
如果 spareParameter为{cookie:'xxxx'} Message.decode解析为spareParameter:'xxxx'
如果 spareParameter为{} Message.decode解析为{}
我使用枚举设置值,但是服务器获取不到值
经过测试发现遇到嵌套消息时,序列化出的二进制数据与用protobuf.js static-module序列化的结果不一致。
encoder.js:73 应该改为
_types[index].encode(ref,writer.uint32((field.id << 3 | 2) >>> 0).fork()).ldelim();
no such Type or Enum 'google.protobuf.Any' in Type 这个能不能支持一下any类型呀
let m = {
requestType: RequestType["heartbeat"],
requestParam: {
// t: 1000000000000
t: 1000
}
}
let r = Request.create(m);
let e = Request.encode(r).finish();
console.log("编码:", m, "解码:", Request.decode(e))
let m1 = {
requestType: RequestType["heartbeat"],
requestParam: {
t: 1000000000000
// t: 1000
}
}
let r1 = Request.create(m1);
let e1 = Request.encode(r1).finish();
console.error("编码:", m1, "解码:", Request.decode(e1))
MiniProgramError
index out of range: 166 + 10 > 166
RangeError: index out of range: 166 + 10 > 166
var msg = pbmsg.encode().finish();
msg.length正确,msg.buffer的长度不对,始终是8192
此问题我通过var base64 = wx.arrayBufferToBase64(msg.buffer);的方式验证过,输出的base64长度不正常,同时,我从服务器端接收的到长度是8192
麻烦帮我看看是什么问题,多谢。
这是最终版本吗?解决了真机不行的情况吗?
syntax = "proto2";
enum WSType {
MQInit = 1; // MQ 初始化
OnlineMsg = 2; // 在线消息
OfflineMsg = 3; // 离线消息
OnlineMsgList = 4; // 在线消息列表
OfflineMsgList = 5; // 离线消息列表
WSRESULTTYPE = 6;
}
message MQConfig {
required string host = 1; // MQ服务器域名
required int32 port = 2; // MQ服务器端口
required string user = 3; // MQ服务器用户名
required string pwd = 4; // MQ服务器密码
required string vhost = 5; // MQ服务器vhost路径
required string mqExchangeName = 6; // MQ在线分发Exchange名称
required string mqQueueName = 7; // MQ离线分发Queue名称
required string mqRoutingKey = 8; // MQ RoutingKey名称
}
message WsMessage {
required WSType type = 1; // 消息类型
optional MQConfig mqconfig = 2; // 如果type是MQInit;MQConfig信息放入此字段
}
代码:
var s = pkgMQInitMsg('1.1.1.1', '1', '1', '1', '1', '1', '1', '1');
var buffer = s.encode().finish();
//buffer[2] = 18;
printDump('encode s: ', s, buffer);
var deMessage = s.decode(buffer);
printDump('decode s', deMessage, buffer);
function pkgMQInitMsg(MqHost, MqPort, MqName, MqPwd, MqExchangeName, MqVHost, MqQueueName, MqRoutingKey) {
var wsmsg = WsMessage.create();
var mqconfig = MQConfig.create();
wsmsg.type = WSType.values.MQInit;
mqconfig.host = MqHost;
mqconfig.port = MqPort;
mqconfig.user = MqName;
mqconfig.pwd = MqPwd;
mqconfig.vhost = MqVHost;
mqconfig.mqExchangeName = MqExchangeName;
mqconfig.mqQueueName = MqQueueName;
mqconfig.mqRoutingKey = MqRoutingKey;
wsmsg.mqconfig = mqconfig;
return wsmsg;
}
小程序端序列化和反序列化都没有问题:
日志:[8, 1, 19, 29, 10, 7, 49, 46, 49, 46, 49, 46, 49, 16, 1, 26, 1, 49, 34, 1, 49, 42, 1, 49, 50, 1, 49, 58, 1, 49, 66, 1, 49]
但是在服务器端解析的时候异常:
AssertionError: Assertion failed
at new goog.asserts.AssertionError (/home/nodeproject/netwhiteboard/nodejs/node_modules/google-protobuf/google-protobuf.js:98:603)
at Object.goog.asserts.doAssertFailure_ (/home/nodeproject/netwhiteboard/nodejs/node_modules/google-protobuf/google-protobuf.js:99:126)
at Object.goog.asserts.assert (/home/nodeproject/netwhiteboard/nodejs/node_modules/google-protobuf/google-protobuf.js:99:385)
at jspb.BinaryReader.readMessage (/home/nodeproject/netwhiteboard/nodejs/node_modules/google-protobuf/google-protobuf.js:345:139)
at Function.proto.com.hengqian.whiteboard.msg.WsMessage.deserializeBinaryFromReader (/home/nodeproject/netwhiteboard/nodejs/whiteboardmsg_pb.js:4290:14)
at Function.proto.com.hengqian.whiteboard.msg.WsMessage.deserializeBinary (/home/nodeproject/netwhiteboard/nodejs/whiteboardmsg_pb.js:4266:54)
at InStream. (/home/nodeproject/netwhiteboard/nodejs/websockeMQ.js:22:49)
at emitNone (events.js:106:13)
at InStream.emit (events.js:208:7)
at endReadableNT (_stream_readable.js:1064:12)
上述日志异常位置是在解析WsMessage的第二个参数mqconfig时出现异常的。
同样的数据,如果我在html5页面生成的消息对象,只有第三位不一样,是18,不是19,如果我在小程序端手动把第三位调成18,服务器端解析没有问题
[8, 1, 19, 29, 10, 7, 49, 46, 49, 46, 49, 46, 49, 16, 1, 26, 1, 49, 34, 1, 49, 42, 1, 49, 50, 1, 49, 58, 1, 49, 66, 1, 49]
帮忙分析一下这个是什么原因?为什么第三位会有差异,这个和google protobuf格式有什么差异?
看了下日志,模拟器上时发送的时候是Uint8Array,真机上Object,不知道是不是这个原因。
微信小程序有越来越多能使用npm的场景了,比如mpvue, taro,还下载源码再使用体验比较差
我是使用您的protobuf.js去解决在小程序中encode的问题,结果经过encode之后发现得到了不同的数据,这个是正常的WEB解析【使用了see: https://github.com/dcodeio/protobuf.js for details】https://raw.githubusercontent.com/keeplight-github/learngit/master/1.png
下面这个是异常的解析,可见得出了不同的数据
https://github.com/keeplight-github/learngit/blob/master/2.png?raw=true
劳驾您给看看
如题,看起来是搞了一个阉割版的pbjs
比如
message account{
string name = 1;
}
var a = {name:""}
编码不应该是 0x18,0x00,应该为空。
当 field为
"BaseReply":{
"fields": {
"spareParameter": {
"keyType": "string",
"type": "string",
"id": 3
},
}
}
这种情况的时候 就会把spareParameter的map类型 解码成string了
跪求怎么解决这种问题
var buffer = msg.encode().finish();
console.log("buffer", buffer);
模拟器上输出的日志:
Uint8Array(92) [8, 1, 18, 88, 10, 14, 49, 56, 48, 46, 49, 53, 48, 46, 49, 56, 54, 46, 52, 50, 16, 168, 44, 26, 6, 104, 113, 121, 120, 109, 113, 34, 8, 49, 50, 51, 121, 120, 51, 50, 49, 42, 3, 47, 121, 120, 50, 20, 51, 95, 49, 53, 52, 49, 49, 52, 57, 49, 55, 54, 57, 51, 49, 50, 57, 55, 50, 56, 58, 20, 51, 95, 49, 53, 52, 49, 49, 52, 57, 49, 55, 54, 57, 51, 49, 50, 57, 55, 50, 56, 66, 0]
而在IOS的微信上输出:
复制不出来,我手动输入在手机看到的日志:
Object {"0": 8, "1": 1, "2": 18, "3": 88 ...
由于刚接触小程序和html5开发,是不是我犯了一个常识性错误,这种对象序列化我如何跨平台通过WSScocket传到服务器去正确解析?
message Response {
int32 command_id = 1;
int32 packet_id = 2;
google.protobuf.Any body = 3 ;
int32 err_code = 4;
string err_msg = 5;
}
会报错 Error: no such Type or Enum 'google.protobuf.Any' in Type .Response
encode之前
timestamp: 1573642868175
decode之后
high: 366
low: 1684837839
unsigned: false
解析 uint64 类型 没有转化成具体的数值,而是 Long 对象类似 {low: 654, high: 0, unsigned: true} ,能在解析时直接转化为数值吗?
全都是int32类型的数据encode之后多了两位
请问执行toObject方法时报错util.copy is not a function怎么解决?
A declarative, efficient, and flexible JavaScript library for building user interfaces.
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
An Open Source Machine Learning Framework for Everyone
The Web framework for perfectionists with deadlines.
A PHP framework for web artisans
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
Some thing interesting about web. New door for the world.
A server is a program made to process requests and deliver data to clients.
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
Some thing interesting about visualization, use data art
Some thing interesting about game, make everyone happy.
We are working to build community through open source technology. NB: members must have two-factor auth.
Open source projects and samples from Microsoft.
Google ❤️ Open Source for everyone.
Alibaba Open Source for everyone
Data-Driven Documents codes.
China tencent open source team.