我在客户端使用kawanet/msgpack-lite (javascript)和msgpack/msgpack-c (C++)将json数据发送到websocketpp服务器,并使用messagepack和messagepack对其进行解析。这样就可以了。
但显然我使用messagepack的方式是错误的,因为我不能正确解析返回的数据。
服务器:
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"}发布于 2015-11-05 20:06:53
std::stringstream的地址不是指向其基础缓冲区的指针。
试试:msg->set_payload(buffer.str());。
发布于 2017-01-02 04:11:56
如果有帮助的话: nlohmann/json现在支持MessagePack (和CBOR),所以你现在可以使用nlohmann/json完全实现你的场景。有关示例,请参阅https://github.com/nlohmann/json#binary-formats-cbor-and-messagepack。
https://stackoverflow.com/questions/33480492
复制相似问题