数据以websocketpp的形式返回为json,但作为带有messagepack的Blob返回

时间:2015-10-31 11:15:09

标签: javascript c++ json c++11 websocket

这很奇怪。我使用用C ++编写的websocketpp修改了websocket-server示例。它使用nlohmann / json解析来自客户端的传入字符串化json数据。数据以json格式返回给客户端,解析并更新hmtl div。到目前为止一切都很好。

然后我想使用messagepack从客户端压缩json数据,解析并以相同的json格式返回它,但现在我得到一个错误,因为数据现在是[object Blob]。我把wireshark打开了,但他们在两种情况下都捕获了完全相同的信息。

两者都以

的形式返回
${"cnt":4,"data":"test","type":"msg"}

并且长度为38。

在Chrome中我收到错误

Uncaught SyntaxError: Unexpected token o

这是添加messagepack之前的客户端:

function sendMessage () {
    var m = document.getElementById("messageField").value;
    var msg = {
        "type": "msg",
        "data": m
    }
    msg = JSON.stringify(msg);
    ws.send(msg);
}

添加了消息包之前的C ++服务器:

void on_message(connection_hdl hdl, server::message_ptr msg) {
    connection_ptr con = m_server.get_con_from_hdl(hdl);

    std::string payload = msg->get_payload();
    try {
        auto jdata = nlohmann::json::parse(payload);

        if (jdata["type"] == "lgn") {
            std::string lname = jdata["data"];
            if (con->name == "") {
                con->name = lname;
            }
        }

        if (jdata["type"] == "msg") {
            std::string clientmsg = jdata["data"];
            jdata["cnt"] = clientmsg.length();
            msg->set_payload(jdata.dump());
            m_server.send(hdl, msg);
        }
    } catch (const std::exception& e) {
        msg->set_payload("Unable to parse json");
        m_server.send(hdl, msg);
        std::cerr << "Unable to parse json: " << e.what() << std::endl;
    }
}

非常基本。以下是添加messagepack后的客户端和服务器:

function sendMessage () {
    var m = document.getElementById("messageField").value;
    var msg = {
        "type": "msg",
        "data": m
    }
    msg = JSON.stringify(msg);
    var buffer = msgpack.encode(msg);
    ws.send(buffer);
}

仅添加

var buffer = msgpack.encode(msg);

并相应地更改ws.send()。

void on_message(connection_hdl hdl, server::message_ptr msg) {
    connection_ptr con = m_server.get_con_from_hdl(hdl);

    std::string payload = msg->get_payload();

    // Parse messagepack
    msgpack::unpacked unpacked_msg;
    msgpack::unpack(&unpacked_msg, payload.data(), payload.size());
    msgpack::object obj = unpacked_msg.get();
    std::string sobj;
    obj.convert(&sobj);

    try {
        auto jdata = nlohmann::json::parse(sobj);

        if (jdata["type"] == "lgn") {
            std::string lname = jdata["data"];
            if (con->name == "") {
                con->name = lname;
            }
        }

        if (jdata["type"] == "msg") {
            std::string clientmsg = jdata["data"];
            jdata["cnt"] = clientmsg.length();
            msg->set_payload(jdata.dump());
            m_server.send(hdl, msg);
        }
    } catch (const std::exception& e) {
        msg->set_payload("Unable to parse json");
        m_server.send(hdl, msg);
        std::cerr << "Unable to parse json: " << e.what() << std::endl;
    }
}

添加

    // Parse messagepack
    msgpack::unpacked unpacked_msg;
    msgpack::unpack(&unpacked_msg, payload.data(), payload.size());
    msgpack::object obj = unpacked_msg.get();
    std::string sobj;
    obj.convert(&sobj);

并相应地更改nlohmann :: json :: parse()。

奇怪的是我只使用messagepack atm。压缩传入的json数据但不能压缩。如果这个问题得到解决,我会两种方式使用它。必须有一些我忽视的东西,并且感谢他们的帮助。

2 个答案:

答案 0 :(得分:1)

也许为时已晚,但nlohmann / json现在也支持MessagePack,因此您可能不需要为MessagePack编码使用特殊库。有关示例,请参阅https://github.com/nlohmann/json#binary-formats-cbor-and-messagepack

答案 1 :(得分:0)

我在客户端的WebSocket.onmessage()中添加了一个FileReader()来提取json数据。

document.addEventListener("DOMContentLoaded", function () {
    if (window.WebSocket) {
        // Let us open a web socket.
        var hname = window.location.hostname;
        ws = new WebSocket("ws://" + hname + ":9002");
        ws.onmessage = function (e) {
            var reader = new FileReader();
            reader.onload = function (e) {
                console.log(reader.result);
                var receivedMsg = JSON.parse(reader.result);
                alert("Message received: " + receivedMsg.data);
                document.getElementById("parsedFromServer").innerHTML = "Text sent: " + receivedMsg.data + ", length: " + receivedMsg.cnt;
            }

            reader.readAsText(e.data);
        }
    } else {
        alert("Websocket unsupported");
    }
});