Node.js(2)-protobuf zeromq gzip

时间:2024-07-07 14:36:56
  • 1、Node.Js环境准备

在win8 + vs.net 2012 环境下调试了很长时间没搞定安装编译问题,重装系统测试了2套环境,解决了编译问题:

1)Win8.1 + vs.net 2013

2) ubuntu server 64位

node.js 依赖库安装调试有点小麻烦,差点就放弃了,就以调试经验大概记录下来。

方案1)Win8.1  64位+ vs.net 2013

  • 安装node.js

http://nodejs.org/download/

  • vs.net 2013集成开发环境

http://nodejstools.codeplex.com/

  • python 2.7 (注意版本,版本过高调试问题非常多)

Our Downloads | Python.org
https://www.python.org/downloads/

  • 设置PATH环境变量

cmd 命令行:node –v 可以测试

方案2)ubuntu server 64位

sudo apt-get update
sudo apt-get install python-software-properties python g++ make
sudo add-apt-repository ppa:chris-lea/node.js
sudo apt-get update
sudo apt-get install nodejs

2.cnpm库安装

国内网络原因,npm安装插件容易出问题,使用cnpm解决。

npm install -g cnpm --registry=http://r.cnpmjs.org
cnpm install [name]

3.protobuf序列化

安装:

cnpm install protobufjs

官方API及介绍:

https://github.com/dcodeIO/ProtoBuf.js

示例google protobuf 描述proto文件:trace.proto

   1:  message Traces{
   2:      repeated Trace Items = 1;
   3:  }
   4:  message Trace {  
   5:          required    string code = 1;  
   6:      required    int32 tradeTime=2;
   7:      optional     double    amount=3;
   8:      optional     int64    volume=4;
   9:      optional     bool    buyOrSell=5;
  10:      optional     int64    deals=6;
  11:      optional     double    price=7;
  12:      enum Exchange
  13:      {        
  14:          Shse=1;
  15:           Szse=2;    
  16:      };
  17:      optional     Exchange    exchange=8;
  18:  }  

4.Zeromq通信

  • 安装Zeromq
1、在http://www.zeromq.org/area:download页面下载最新的zeromq .
wget http://download.zeromq.org/zeromq-XXXXX.tar.gz
2
、安装相关软件
$ sudo apt-get install libtool autoconf automake
$ sudo apt-get install uuid-dev g++
$ sudo apt-get install python-dev
3、编译安装zmq
$ ./configure
$ make
$ sudo make install
$ sudo ldconfig

  • 安装zmq for node

cnpm install zmq

官方API介绍:

Node.js Binding - zeromq
http://zeromq.org/bindings:node-js

测试代码:

   1:  var protoBuf = require("protobufjs");
   2:  //protobufjs test
   3:  var builder  = protoBuf.loadProtoFile("trace.proto");
   4:  var     Rtquote =builder.build();
   5:   
   6:  var zlib = require('zlib');
   7:  var zmq = require('zmq');
   8:  var socket = zmq.socket('sub');
   9:  var port = "tcp://192.168.1.16:9178";
  10:  socket.identity = 'subscriber' + process.pid;
  11:  socket.connect(port);
  12:  console.log('connected!');
  13:   
  14:  socket.subscribe('Trace');
  15:   
  16:  var  i = 0;
  17:  socket.on('message', function(data) {
  18:       /*decode*/
  19:      var buf=data;
  20:          //console.log("packetType: " + buf.toString('utf8', 0, 26));
  21:            
  22:          //2.解压缩  自定义协议部分
  23:          var bodyBuffers = buf.slice(62,data.length);
  24:          //console.log("bodylength3:"+bodyBuffers.length);
  25:      
  26:          /*
  27:          if(bodyBuffers.length!=bodyLength)
  28:          {
  29:  
  30:              console.log(bodyBuffers.length+"!="+bodyLength);
  31:          }
  32:         */
  33:         
  34:          //body Gzip解压缩
  35:          zlib.gunzip(bodyBuffers,function(err, buffer) {
  36:            if (!err) {
  37:                  //console.log("gzip:"+buffer.length);
  38:                  var items =     Rtquote.Traces.decode(buffer);      
  39:                  //console.log("len:"+items.Items.length);    
  40:                  for(var i=0;i<items.Items.length;i++)
  41:               {
  42:                                  var item=items.Items[i];
  43:                                 console.log("code: "+item.code+" tradetime: "+item.tradeTime+" amount: "+item.amount+" volume: "+item.volume+" buyOrSell: "+item.buyOrSell+" deals: "+item.deals+" price: "+item.price+" exchange: "+item.exchange);
  44:               }                        
  45:            }
  46:      });
  47:  });
  48:   
  49:  console.log('Hello world');

运行:sudo node app.js 可以实时使用Zmq 接收数据,解压 ,序列化。

Node.js(2)-protobuf  zeromq gzip

参考资料:

入门知识概览 · cnodejs/nodeclub Wiki · GitHub
https://github.com/cnodejs/nodeclub/wiki/%E5%85%A5%E9%97%A8%E7%9F%A5%E8%AF%86%E6%A6%82%E8%A7%88