我正在尝试使用 Apache Thrift 在以不同语言实现的应用程序之间传递消息。它不一定用作 RPC,但更多用于序列化/反序列化消息。 一个应用程序在 node.js 中。我试图弄清楚 Apache thrift 如何与 node.js 一起工作,但我找不到太多的文档和示例,除了关于 Cassandra 的一小部分: https://github.com/apache/thrift/tree/trunk/lib/nodejs
同样,我不需要在 .thrift 文件中声明任何过程,我只需要序列化一个简单的数据结构,例如:
struct Notification {
1: string subject,
2: string message
}
谁能帮我举个例子?
最佳答案
在浪费大量时间查看 nodejs 库之后,我终于找到了这个问题的答案。
//SERIALIZATION:
var buffer = new Buffer(notification);
var transport = new thrift.TFramedTransport(buffer);
var binaryProt = new thrift.TBinaryProtocol(transport);
notification.write(binaryProt);
此时,可以在transport.outBuffers字段中找到字节数组:
var byteArray = transport.outBuffers;
对于反序列化:
var tTransport = new thrift.TFramedTransport(byteArray);
var tProtocol = new thrift.TBinaryProtocol(tTransport);
var receivedNotif = new notification_type.Notification();
receivedNotif.read(tProtocol);
还需要将以下行添加到 nodejs 库中的 index.js 文件中以实现节俭:
exports.TFramedTransport = require('./transport').TFramedTransport;
exports.TBufferedTransport = require('./transport').TBufferedTransport;
exports.TBinaryProtocol = require('./protocol').TBinaryProtocol;
此外,nodejs 库中至少还存在一个错误。
关于node.js - Apache Thrift 与 nodejs 示例,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13344819/