nodejs的protoBuf使用

來源:互聯網
上載者:User

標籤:nodejs   protobuf   

protobuf的使用:

項目地址:https://github.com/chrisdew/protobuf

peed

The Protobuf for Node add-on relies on the protobuf C++ runtime but it does not require any generation, compilation or linkage of generated C++ code. It works reflectively and is thus able to deal with arbitrary schemas. This means however, that it is not as fast as with generated code. Simple measurements show that unmarshalling is about between 20% and 50% faster than V8‘s native JSON support.

Calling C++ services is faster since it avoids marshalling to bytes but transfers data directly between the JS objects and the protocol messages. Also, in this case you compile the generated service interface into your code, so reflection is faster.

Building and running

You need to have node.js 0.4 and protobuf 2.4.0a installed. protobuf_for_node depends on the respective versions of v8 and eio shipped by node. Use branch node2 for node0.2/protobuf 2.3.0. If you want to use a later protobuf version, you may need to re-generate the example protoservice code: cd example; protoc --cpp_out=. protoservice.proto.

To build protobuf for node, do:

PROTOBUF=<protobuf prefix> /path/to/node/bin/node-waf configure clean build

This will build a) the protobuf_for_node library (build/default/libprotobuf_for_node_lib.so). It is linked to by your service-exporting add-ons and by the b) protobuf_for_node add-on (build/default/protobuf_for_node.node).

You need a) and the protobuf library in your DYLD_LIBRARY_PATH and b) in your NODE_PATH if you require(‘protobuf_for_node‘).

 

1.

需要一個*.proto檔案,可以產生desc檔案,是使用google的一個命令列工具:protoc --descriptor_set_out=buftest.desc --include_imports buftest.proto產生的。運行了出錯,沒有這個命令列工具。尋找下載,發現無法訪問網站,需要設定代理,設定代理後:具體教程在:http://dbua.iteye.com/blog/1633079,http://www.cnblogs.com/Anker/archive/2013/07/24/3209764.html,已測試可以正常產生desc檔案

*.proto檔案基本格式如下:message Person {
required string name = 1;
required int32 id = 2;
ptional string email = 3;
enum PhoneType {
MOBILE = 0;
HOME = 1;
WORK = 2;
}
message PhoneNumber {
required string number = 1;
optional PhoneType type = 2 [default = HOME];
}
repeated PhoneNumber phone = 4;
}

.proto檔案編寫規則:

Proto支援的資料類型:http://blog.csdn.net/superbfly/article/details/17920383

1)對於required的欄位而言,初值是必須要提供的,否則欄位的便是未初始化的。在Debug模式的buffer庫下編譯的話,序列化話的時候可能會失敗,而且在還原序列化的時候對於該欄位的解析會總是失敗的。所以,對於修飾符為required的欄位,請在序列化的時候務必給予初始化。

    2)對於optional的欄位而言,如果未進行初始化,那麼一個預設值將賦予該欄位,當然也可以指定預設值,如上述proto定義中的PhoneType欄位類型。

    3)對於repeated的欄位而言,該欄位可以重複多個,google提供的這個addressbook例子便有個很好的該修飾符的應用情境,即每個人可能有多個電話號碼。在進階語言裡面,我們可以通過數組來實現,而在proto定義檔案中可以使用repeated來修飾,從而達到相同目的。當然,出現0次也是包含在內的。      

    其中欄位標籤標示了欄位在二進位流中存放的位置,這個是必須的,而且序列化與還原序列化的時候相同的欄位的Tag值必須對應,否則還原序列化會出現意想不到的問題。

Prot檔案編寫範例:http://my.oschina.net/cxh3905/blog/293000

2.在項目中使用‘protobuf‘,結合我們編寫的.protobuf檔案產生的desc檔案,對資料進行序列化。(使用demo測試成功)

代碼:

var fs = require(‘fs‘);var Schema = require(‘protobuf‘).Schema; // "schema" contains all message types defined in buftest.proto|desc.var schema = new Schema(fs.readFileSync(‘buftest.desc‘)); // The "BufTest" message.var BufTest = schema[‘com.chrisdew.buftest.BufTest‘]; var ob = { num: 42 };ob.payload = new Buffer("Hello World"); var proto = BufTest.serialize(ob);console.log(‘proto.length:‘, proto.length); var outOb = BufTest.parse(proto);console.log(‘unserialised:‘, JSON.stringify(outOb)); var payload = new Buffer(outOb.payload);console.log(payload);

 

如果有和用戶端進行資料轉送,用戶端也必須有對應解碼檔案,才能對我們發過去的資料進行解碼,as3的在這裡有說明:http://bbs.9ria.com/thread-62331-1-1.html。

protoBuf更詳細的教程:http://www.ibm.com/developerworks/cn/linux/l-cn-gpb/

 


本文出自 “jie” 部落格,請務必保留此出處http://zdok1024.blog.51cto.com/1656266/1539460

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.