1、安装protobufjs
npm install protobufjs
2、helloworld.proto 实例
syntax = "proto3";
//包名
package hellopackage;
//类
message HelloWorld {
// [type] [key] = [tag]
string message = 1;
}
3、js代码使用 helloworld.js
const protobuf = require("protobufjs");
//加载 helloworld.proto
protobuf.load("./helloworld.proto", function (err, root) {
if (err) throw err;
// lookupType(包名.类名)
const helloMessage = root.lookupType("hellopackage.HelloWorld");
const payload = {message:"Hello Girl"};
//序列化
const message1 = helloMessage.create(payload);
const buffer = helloMessage.encode(message1).finish();
//反序列化
const message2 = helloMessage.decode(buffer);
const object = helloMessage.toObject(message2);
console.log(buffer);
console.