Skip to content

编写proto文件

新建message.proto文件

示例

js
syntax = "proto3";

message PushFrame{
  int64 seqid = 1;
  int64 logid = 2;
  int64 service = 3;
  int64 method = 4;
  repeated PushHeader headersList = 5;
  string payloadEncoding = 6;
  string payloadType = 7;
  bytes payload = 8;
}

message PushHeader{
  string key = 1;
  string value = 2;
}


message Response{
    repeated Message messagesList = 1;
    string cursor = 2;
    int64 fetchInterval = 3;
    int64 now = 4;
    string internalExt = 5;
    int32 fetchType = 6;
    map<string, string> routeParams = 7;
    int64 heartbeatDuration = 8;
    bool needAck = 9;
    string pushServer = 10;
}

message Message{
    string method = 1;
    bytes payload = 2;
    int64 msgId = 3;
    int32 msgType = 4;
    int64 offset = 5;
}

将proto转json或js

npx pbjs -t json-module -w commonjs message.proto > message.js

npx pbjs -t json message.proto > message.json

使用上面命令记得安装npm install protobufjs-cli

使用解密rpc信息

js
const protobuf = require("protobufjs");
var root = protobuf.Root.fromJSON(require("./message.json"));
let PushFrame = root.lookup("PushFrame")
let Response = root.lookupType("Response")

// 反序列化
var buf = Buffer.from(params.response.payloadData, 'base64');
let o = PushFrame.decode(new Uint8Array(buf))