Skip to content
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

Closed
DavidLvSinonet opened this issue Nov 18, 2015 · 3 comments

Comments

@DavidLvSinonet
Copy link

Hi @dcodeIO

There is the protobuf define in myTest.proto:

message SingleCard {
    required string cmd = 1;
    optional int32 userId=2;
    required string gameId = 3;
    required int32 card = 4;
    required int32 index = 5;
    required string roundNo = 6;
    optional int32 timestamp=7;
    required int32 gameType=8;
    repeated int32 userIds = 100;
}

And here is the codes to decode:

        var ProtoBuf = dcodeIO.ProtoBuf;
        var builder = ProtoBuf.loadProtoFile("myTest.proto"),
        var SingleCard = builder.build("SingleCard");

        var respData = new Uint8Array([10, 10, 83, 105, 110, 103, 108, 101, 67, 97, 114, 100, 18, 2, 65, 49, 26, 15, 65, 49, 45, 49, 53, 49, 49,
            49, 56, 49, 50, 49, 56, 52, 50, 32, 135, 165, 197, 198, 145, 42, 40, 1, 48, 2, 56, 1]);
        var card = SingleCard.decode(respData.buffer);
        console.log(card);

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

@jiangtao
Copy link

Thanks @dcodeIO
I has the same problem. My test code like:

test.proto

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;
}

javascript code

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);
error:Uncaught Error: Illegal wire type for unknown field 13 in Message .MsgHeader#decode: 7

@dcodeIO
Copy link
Member

dcodeIO commented Nov 18, 2015

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

@DavidLvSinonet
Copy link
Author

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants