node.js - Apache Thrift 与 nodejs 示例

标签 node.js serialization deserialization thrift

我正在尝试使用 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/

相关文章:

node.js - 使用 ELB 和 Node 防止 X-Forwarded-For 欺骗

c# - C#中涉及引用时的序列化与反序列化

c# - 将 xml 反序列化为来自 webapi 的对象

JSON Oracle SQL 解析/取消嵌套转义形式的嵌入 JSON 数据

c++ - 没有 Boost.Serialization 的序列化

c# - 如何确定类是否具有 DataContract 属性?

javascript - Node js 中的异步编程通过 mongoose 模型传递常量/预定义的强制值

javascript - 使用 C++ 中的构造函数时 ffi 的返回类型是什么

node.js - 如何在 Node.js 中安全地运行用户提交的脚本

java - Tomcat 返回错误版本的文件