如何在protobuf.js中构造扩展消息

时间:2015-01-27 07:20:38

标签: javascript node.js protocol-buffers

根据此post,我已定义了Protocol Buffer格式的几条消息,如下所示

message MsgHead
{
    extensions 80 to max;

    enum Type
    {
        REQ = 1;
        RESP = 2;
    }
    required string version = 1;
    required Type type = 2;
}

message Req
{
    extend MsgHead
    {
        required Req msg = 81; // unique message extend number
    }

    optional string id = 1;
    optional string ip_address = 2;
}

message Resp
{
    extend MsgHead
    {
        required Resp msg = 82;
    }

    optional string id = 1;
    optional string time = 2;
}

ProtoBuf.js用于构造和解析这些自定义消息。

    var req = new Req({
        "id": "60"
        "ip_address": "172.17.67.60"
    });

    var msg_head = new Header();
    msg_head.set("version", '1.0');
    msg_head.set('type', 1);
    msg_head.set('.Req.msg', req);
    // how to construct req message within msg_head?

如何直接从Req构建MsgHead消息,就像req = msg_head.Extensions[Req.msg]一样?

1 个答案:

答案 0 :(得分:0)

引自this

msg_head['.Req.msg']

用于从MsgHead获取Req消息。