2014-02-17 75 views
0

試圖瞭解更多關於流和cassandra的知識,所以想通過二進制協議。我甚至無法迴應。使用節點流得到cassandra二進制協議的響應

var net = require('net'); 
var util = require('util'); 
var stream = require('stream'); 

var session; 
session = net.connect({ port: 9160 }, function() { 
    console.log('connected'); 

    var header = new Buffer([ 0x01, 0x00, 0x01, 0x01 ]); 
    var length = new Buffer(4); 
    var body = new Buffer("{'CQL_VERSION':'3.0.0'}", 'utf8'); 
    length.writeUInt32BE(body.length, 0); 

    session.write(header); 
    session.write(length); 
    session.write(body); 

    setTimeout(function() { session.end(); }, 5000); 
}); 

session.on('error', function (err) { 
    console.log(err); 
}); 

var client = new (stream.Writable); 

client._write = function (chunk, _, next) { 
    console.log('response received'); 
    next(); 
}; 

session.pipe(client); 

該程序運行5秒鐘,幾乎立即打印「連接」,但可寫入的流不會從數據庫接收內容。任何幫助將非常感激!


我工作過這個documentation for the binary protocolthis guide to node streams的。

Cassandra正在運行,版本號2.0.5

回答

0

9160是節儉的港口。你是否用端口9042試過你的代碼?

+0

就是這樣,謝謝! – AJcodez