-
Notifications
You must be signed in to change notification settings - Fork 1.4k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
I have a problem with optional field. (Illegal wire type for field Message.Field) #358
Comments
Thanks @dcodeIO
message MsgHeader {
optional int32 msgType = 10000;
optional int32 seq = 2;
optional int32 protocolVersion = 3;
optional ClientType clientType = 4;
optional int32 businessType = 5;
optional int32 bodyLength = 6;
optional int64 time=7;
optional string DeviceID=8;
optional string token=9;
enum ClientType {
ANDROID = 0;
IPHONE = 1;
WEB = 2;
}
}
message RegisterRequestMsg {
optional string userName = 1;
optional string passWord = 2;
}
var builder = window.dcodeIO.ProtoBuf.loadProtoFile('./test.proto');
var MessageHeader = builder.build('MsgHeader');
var MessageBody = builder.build('RegisterRequestMsg');
var ByteBuffer = window.dcodeIO.ByteBuffer;
var _getHeader = function (options) {
var settings = {
businessType: 1,
DeviceID: '1',
token: 'test_token',
protocolVersion: 1,
time: new Date().getTime(),
msgType: 20000,
seq: 10000
};
for (var i in settings) {
if (typeof options[i] == 'undefined') {
options[i] = settings[i]
}
}
return new MessageHeader(options);
};
var _getBody = function () {
var params = {
userName: 'hello',
passWord: 'world'
};
return new MessageBody(params);
};
// one completed message: (headerByteLength + headerBuf + bodyBuf)
var encodeMessage = function () {
var bodyProbuf = _getBody();
var headerProbuf = _getHeader({
bodyLength: bodyProbuf.calculate()
});
var bb = new ByteBuffer();
var bufArray = [];
var concatBuf;
console.log('encode header length: %d', headerProbuf.calculate())
bb.writeInt32(headerProbuf.calculate());
bufArray.push(bb.buffer, bodyProbuf.toBuffer(), headerProbuf.toBuffer());
concatBuf = ByteBuffer.concat(bufArray);
concatBuf.printDebug();
return concatBuf.buffer;
};
var decodeMessage = function (buf) {
var bb = ByteBuffer.concat([buf]);
var headerLengthByte = 4;
var headerLength = bb.readInt32(0);
var headerEnd = headerLengthByte + 1 + headerLength;
console.log('decode header length: %d', headerLength);
var headerByteBuffer = bb.slice(headerLengthByte + 1, headerEnd);
headerByteBuffer.printDebug();
var headerJSON = MessageHeader.decode(headerByteBuffer);
var bodyByte = headerJSON.bodyLength;
var bodyEnd = headerEnd + 1 + bodyByte;
var bodyJSON = MessageBody.decode(bb.slice(headerEnd + 1 , bodyEnd));
console.log('header bytes %d', headerByte);
console.log('body bytes %d', bodyByte);
console.log('header json', headerJSON);
console.log('body json', bodyJSON);
};
var msg = encodeMessage();
decodeMessage(msg);
|
Usually, errors like these result from the binary data being corrupted. There is a wiki page on that topic: https://github.com/dcodeIO/protobuf.js/wiki/How-to-read-binary-data-in-the-browser-or-under-node.js%3F Additionally, a couple of examples on how to check the binary data by hand are available here: #55 |
Thanks @dcodeIO I will check the message that send from server, I have tested that use SingleCard to encode to binary array and then decode this array to SingleCard object, it is correct. I think the response array is incorrect. |
Hi @dcodeIO
There is the protobuf define in myTest.proto:
And here is the codes to decode:
The respData is received from Sever.
error: Uncaught Error: Illegal wire type for field Message.Field .SingleCard.userId: 2 (0 expected)
Sorry, I'm a beginner of protobuf.js.
Thanks
David
The text was updated successfully, but these errors were encountered: