0

我正在使用客户端上的 kawanet/msgpack-lite (javascript) 和 msgpack/msgpack-c (C++) 将 json 数据发送到带有消息包的 websocketpp 服务器来解压它,并使用 nlohmann/json 在服务器上解析它。这很好。

但我显然以错误的方式使用消息包,因为我无法正确解析返回的数据。

服务器:

if (jdata["type"] == "msg") {
    std::stringstream buffer;
    std::string clientmsg = jdata["data"];
    jdata["cnt"] = clientmsg.length();
    msgpack::pack(buffer, jdata.dump());
    size_t plen = buffer.tellp();
    msg->set_payload(&buffer, plen);
    m_server.send(hdl, msg);
}

客户:

reader.onload = function (e) {
    console.log("FileReader.onload(): " + reader.result);
    var decoded_message = msgpack.decode(reader.result);
}
reader.readAsText(e.data);

它在 msgpack.decode() 上失败了

Uncaught Error: Invalid type: 0xh

在 set_payload() 中将 json 作为字符串发送时

msg->set_payload(jdata.dump());

传输正常

FileReader.onload(): {"cnt":4,"data":"test","type":"msg"}
4

2 回答 2

0

a 的地址std::stringstream不是指向其底层缓冲区的指针。

试试:msg->set_payload(buffer.str());

于 2015-11-05T12:06:52.993 回答
0

如果有帮助:nlohmann/json 现在支持 MessagePack(和 CBOR),因此您现在可以完全使用 nlohmann/json 实现您的场景。有关示例,请参见 https://github.com/nlohmann/json#binary-formats-cbor-and-messagepack

于 2017-01-01T20:11:56.460 回答