所以我使用 boost::beast 作为 WebSocket 服务器。我想接收二进制消息并使用 nlohmann::json 解析它。但是我收到一条错误消息:
3 个重载都不能转换参数“nlohmann::detail::input_adapter”
这是一些代码:
boost::beast::flat_buffer buffer;
ws.read(buffer);
if (!ws.got_text()) {
ws.text(false);
json request = json::from_msgpack(msgpack);
ws.write( json::to_msgpack(request) ); // echo request back
}
Run Code Online (Sandbox Code Playgroud)
如果我尝试静态转换为 std::vector 我得到: E0312 / 没有合适的用户定义转换
boost::beast::flat_buffer buffer;
ws.read(buffer);
if (!ws.got_text()) {
ws.text(false);
boost::asio::mutable_buffer req = buffer.data();
//unsigned char* req2 = static_cast<unsigned char*>(req); // does not work
//std::vector<std::uint8_t> req2 = static_cast<std::vector<std::uint8_t>>(req); // does not work
json request = json::from_msgpack(buffer.data());
ws.write(boost::asio::buffer(json::to_msgpack(request)));
}
Run Code Online (Sandbox Code Playgroud)
如何从缓冲区中获取二进制数据以便 nkohman::json 可以解析它?