首页 > 解决方案 > nlohmann/json uWebsocket

问题描述

对于一个项目,我想将该uNetworking/uWebSockets库与该库结合使用nlohmann/json

到目前为止,这是我的代码:

#include <uWS/uWS.h>
using namespace uWS;
using json = nlohmann::json;

int main() {
    Hub h;

    h.onMessage([](WebSocket<SERVER> *ws, char *message, size_t length, OpCode opCode) {
        json parsed = json::parse(message);
        ws->send(message, length, opCode);
    });

    if (h.listen(3000)) {
        h.run();
    }
}

解析消息(经过验证的 json)时出现此错误:

libc++abi.dylib: terminating with unexpected exception of type nlohmann::detail::parse_error: [json.exception.parse_error.101] parse error at 37: syntax error - invalid literal; last read: '"{ \"happy\": true, \"pi\": 3.141 }"o'; expected end of input

到目前为止,我被困住了。

提前致谢。

西奥

标签: c++jsonwebsocket

解决方案


我找不到任何文档,uWebSockets但大概是因为onMessage参数length不一定message以空值终止。因此,您需要这样做:

json parsed = json::parse(message, length);

推荐阅读