nodejs使用protobufjs

1、使用前提
nodejs会发送mqtt消息,但是消息体要进行压缩,未使用json或者XML,我们使用protobufjs来压缩数据。
2、使用步骤
(1)安装protobufjs

//全局安装
npm install -g protobufjs    
//非全局安装
npm install protobufjs    

(2)创建protp/config.proto文件

syntax = "proto3";
message Config{
  int32 param=1;
  string req_id=2;
}

(3)使用命令,将config.proto转换成js文件

cd proto    //进入到proto文件夹
//参数说明-o生成的文件,config.proto是源文件,config.js就是生成的文件,直接使用要是全局安装
pbjs -t static-module -w commonjs -o config.js config.proto
//未全局安装的话,使用以下命令 其中./pbjs在node_module的./bin中,建议使用上一种,方便快捷
./pbjs -t static-module -w commonjs -o ../../server/protocol/C2SProtocol.js ../../server/protocol/C2SProtocol.proto

将获取这样的文件:config.js
tips:
经过转换你将发现,通过pbjs命令将.proto文件转换成proto.js文件message属性key值下划线自动转换成了大写字母。如定义的req_id,在js中就定义为reqId了。

"use strict";
var $protobuf = require("protobufjs/minimal");

var $Reader = $protobuf.Reader, $Writer = $protobuf.Writer, $util = $protobuf.util;
var $root = $protobuf.roots["default"] || ($protobuf.roots["default"] = {});
$root.Config = (function() {
    function Config(properties) {
        if (properties)
            for (var keys = Object.keys(properties), i = 0; i < keys.length; ++i)
                if (properties[keys[i]] != null)
                    this[keys[i]] = properties[keys[i]];
    }

    Config.prototype.param = 0;
    Config.prototype.reqId = "";
    Config.create = function create(properties) {
        return new Config(properties);
    };
    Config.encode = function encode(message, writer) {
        if (!writer)
            writer = $Writer.create();
        if (message.param != null && Object.hasOwnProperty.call(message, "param"))
            writer.uint32(/* id 1, wireType 0 =*/8).int32(message.param);
        if (message.reqId != null && Object.hasOwnProperty.call(message, "reqId"))
            writer.uint32(/* id 2, wireType 2 =*/18).string(message.reqId);
        return writer;
    };
    Config.encodeDelimited = function encodeDelimited(message, writer) {
        return this.encode(message, writer).ldelim();
    };
    Config.decode = function decode(reader, length) {
        if (!(reader instanceof $Reader))
            reader = $Reader.create(reader);
        var end = length === undefined ? reader.len : reader.pos + length, message = new $root.Config();
        while (reader.pos < end) {
            var tag = reader.uint32();
            switch (tag >>> 3) {
            case 1:
                message.param = reader.int32();
                break;
            case 2:
                message.reqId = reader.string();
                break;
            default:
                reader.skipType(tag & 7);
                break;
            }
        }
        return message;
    };
    Config.decodeDelimited = function decodeDelimited(reader) {
        if (!(reader instanceof $Reader))
            reader = new $Reader(reader);
        return this.decode(reader, reader.uint32());
    };
    Config.verify = function verify(message) {
        if (typeof message !== "object" || message === null)
            return "object expected";
        if (message.param != null && message.hasOwnProperty("param"))
            if (!$util.isInteger(message.param))
                return "param: integer expected";
        if (message.reqId != null && message.hasOwnProperty("reqId"))
            if (!$util.isString(message.reqId))
                return "reqId: string expected";
        return null;
    };
    Config.fromObject = function fromObject(object) {
        if (object instanceof $root.Config)
            return object;
        var message = new $root.Config();
        if (object.param != null)
            message.param = object.param | 0;
        if (object.reqId != null)
            message.reqId = String(object.reqId);
        return message;
    };
    Config.toObject = function toObject(message, options) {
        if (!options)
            options = {};
        var object = {};
        if (options.defaults) {
            object.param = 0;
            object.reqId = "";
        }
        if (message.param != null && message.hasOwnProperty("param"))
            object.param = message.param;
        if (message.reqId != null && message.hasOwnProperty("reqId"))
            object.reqId = message.reqId;
        return object;
    };
    Config.prototype.toJSON = function toJSON() {
        return this.constructor.toObject(this, $protobuf.util.toJSONOptions);
    };

    return Config;
})();

module.exports = $root;
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值