Source Link: JavaScript implementation of Protobuf.js–protocol buffers
Recommended front end URL:http://keenwon.com/
The most common data format in JavaScript is Json,xml, but these two formats have significant deficiencies in data transmission. and protocol buffers can be a good solution to this problem, the following reference to the encyclopedia of the definition of protocol buffers:
Protocol buffers is a data description language developed by Google, similar to the ability of XML to serialize structured data for data storage, communication protocols, and more. It is not dependent on language and platform and is extremely extensible. At this stage, the official support of C + +, JAVA, Python and other three programming languages, but can find a large number of almost all languages of the third-party expansion package.
The same as the data storage format, compared to JSON, Protocol buffers advantages are mainly reflected in the performance and volume, performance needs actual testing, for the moment, the data volume advantages are more obvious, for example, a JSON file:
- [
- { "Name" : "Zhangsan" , "Gender" : 0, "age" : 18},
- {"Name": "Lisi", "Gender": 1, "age": 19 }
- ]
Then look at a protobuf file:
- Message person {
- Required string Name = 1;
- Optional int32 Gender = 2;
- Optional Int32 age = 3;
- }
The problem with JSON files is that there are too many invalid data, such as Name
Gender
, and so on, that the content is repeatedly repeated, making the data larger in size. Then look at the Protobuf file, which uses a unique ID (number) instead of the complex key in JSON, so as long as the data sender and the receiver are the same set of "template" to parse the data, it can greatly shorten the volume of the message.
Protobuf.js
Protobuf.js is a bytebuffer.js-based protocol buffers pure JavaScript implementation. The main functions are parsing .proto
files, building the message class, and simply encoding and decoding. I am currently using the PROTOBUF format in a node-webkit for data interaction on the server side (the server is a PROTOBUF that is implemented according to the old C + + client requirements).
At present, the main use of protobuf.js is to read the old .proto
file, create a message class, encode, send to the server, as follows:
user.proto
File:
- Package protobuf;
- Message Usermodel {
- Required string cyuserno = 1;
- Required string cypassword = 2;
- Required string cystatus = 3;
- }
Nodejs Code:
- var fs = require("FS"),
- protobuf = require("Protobufjs"),
- Userprotostr = fs. Readfilesync('./user.proto '). ToString(),
- Usermodel = protobuf. Loadproto(userprotostr). Build(' protobuf '). Usermodel,
- Usermodel;
- Usermodel= new Usermodel();
- Usermodel. Set(' Cyuserno ', ' 111111 ');
- Usermodel. Set(' Cypassword ', ' 123456 ');
- Usermodel. Set(' cystatus ', ' 1 ');
- var buffer = Usermodel. Encode(). Tobuffer();
The decoding method is also very simple:
- var userInfo = usermodel. Decode(data);
- UserInfo. Get(' Cyuserno ');
- ......
In addition Protobuf.js also provides some tools, such .proto
as .json
the conversion and so on, specific to his official website: https://github.com/dcodeIO/ProtoBuf.js
Finish
Protobuf.js–protocol buffers JavaScript implementation (RPM)