Reputation: 1138
I am trying to send a message over MQTT making use of the google protocol buffer in javascript (ProtoBuf.js)
I was able to encode the message using the following code:
var ProtoBuf = dcodeIO.ProtoBuf;
var builder = ProtoBuf.loadProtoFile("./complex.proto"),
Game = builder.build("Game"),
Car = Game.Cars.Car;
var car = new Car({
"model" : "Rusty",
"vendor" : {
"name" : "Iron Inc.",
"address" : {
"country" : "USa"
}
},
"speed" : "FAST"
});
var buffer = car.encode();
console.log(buffer);
var messagegpb = buffer.toBuffer();
console.log(messagegpb ); //This prints "ArrayBuffer { byteLength: 29 }"
Now for decoding when I tried the following, it just doesn't do anything. I see no logs in the browser as well.
var dec = builder.build("Game"); //nothing after this line gets executed
var msg = dec.decode(messagegpb);
console.log(msg);
This is the link of the .proto file I am using. https://github.com/dcodeIO/ProtoBuf.js/blob/master/tests/complex.proto
Could someone point me where I am going wrong?
Thanks a ton
Upvotes: 4
Views: 9199
Reputation: 45326
Presumably these lines:
var dec = builder.build("Game");
var msg = dec.decode(messagegpb);
Need to be:
var Game = builder.build("Game");
var msg = Game.Cars.Car.decode(messagegpb);
That is, you need to specify what type you're decoding.
Probably your attempt to call dec.decode
was throwing an exception saying the decode
method didn't exist. You should have been able to see these exceptions on the error console, or caught them with try
/catch
.
Upvotes: 2