c++ - 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
到目前为止,我被困住了。
提前致谢。
西奥
解决方案
我找不到任何文档,uWebSockets
但大概是因为onMessage
参数length
不一定message
以空值终止。因此,您需要这样做:
json parsed = json::parse(message, length);
推荐阅读
- javascript - 从 React 连接到 MySQL 数据库?
- spring-boot - Spring Boot 2.5.3 - SSL 密码套件
- git - 无法删除名为 release 的 Git 分支,并阻止创建以 release/ 为前缀的分支
- visual-studio - 在VS2019中,调试时自动改变变量值?
- scrollview - Android Studio - 如何将滚动功能添加到指南约束?
- amazon-web-services - 使用 Terraform 在 EKS 上部署 AWS 负载均衡器控制器
- git - 如果员工离开公司并删除了 Github 上的 GPG 密钥,如何保持签名提交的验证?
- javascript - $(document).height() - 滚动后改变值
- python - Python Pandas 中的数据框转换,其中每个单元格由竖线分隔
- sql - 有没有办法在下面的 SQL 代码上设置条件?我只想显示到年底,但现在显示的是开始年 + 5 年