首页 > 解决方案 > node.js 到 c++ 协议缓冲区

问题描述

我在 node.js 中创建了一个协议缓冲区,我想在 node c++ 插件中对其进行解码。

在节点中,我创建缓冲区并将其发送到 c++ 插件:

var Message = root.lookupType(tutorial.AddressBook);
var payload = { people: [{name: "name", id: 23, email: "sdfsdf", phones: [{ number: "12", type: 1}], last_updated: 3}]};

var errMsg = Message.verify(payload);
if (errMsg)
    throw Error(errMsg);

var message = Message.create(payload); 

var buffer = Message.encode(message).finish();
const cppResult = addons.myNanMethod(buffer);

那么我应该如何在 C++ 中解码呢?ParseFromString 没有帮助。我收到运行时错误“节点:符号查找错误:path/to/my/addon。”

char* data = (char*) node::Buffer::Data(info[0]->ToObject());
std::string datastring data, node::Buffer::Length(info[0])););

// Verify that the version of the library that we linked against is
// compatible with the version of the headers we compiled against.
GOOGLE_PROTOBUF_VERIFY_VERSION;

tutorial::AddressBook address_book;
{
    // Read the existing address book.
    if (!address_book.ParseFromString(&datastring)) {
        cerr << "Failed to parse address book." << endl;
        errorCode = 1;
    }
}

标签: c++node.jsprotocol-buffers

解决方案


推荐阅读