如何将助推兽multi_buffer转换为字符串?
How to convert boost beast multi_buffer to string?
我从boost::beast网站复制websocket示例并运行它Websocket会话工作正常,但我不知道如何将收到的multi_buffer转换为字符串。
下面的代码是 Websocket 会话处理程序。
void
do_session(tcp::socket &socket) {
try {
// Construct the stream by moving in the socket
websocket::stream <tcp::socket> ws{std::move(socket)};
// Accept the websocket handshake
ws.accept();
while (true) {
// This buffer will hold the incoming message
boost::beast::multi_buffer buffer;
// Read a message
boost::beast::error_code ec;
ws.read(buffer, ec);
if (ec == websocket::error::closed) {
break;
}
// Echo the message back
ws.text(ws.got_text());
ws.write(buffer);
}
cout << "Close" << endl;
}
catch (boost::system::system_error const &se) {
// This indicates that the session was closed
if (se.code() != websocket::error::closed)
std::cerr << "Error: " << se.code().message() << std::endl;
}
catch (std::exception const &e) {
std::cerr << "Error: " << e.what() << std::endl;
}
}
有没有办法将缓冲区转换为字符串?
由于最初的问题是关于直接转换为字符串,而不使用流,我决定添加我的答案。
您可以使用beast::buffers_to_string(buffer.data())
.
您可以在buffer.data()
上使用缓冲区
std::cout << "Data read: " << boost::beast::buffers(buffer.data()) <<
std::endl;
相关文章:
- 防止主数据类型C++的隐式转换
- 模板参数替换失败,并且未完成隐式转换
- 努力将整数转换为链表。不知道我在这里做错了什么
- HEX值到wchar_t字符(UTF-8)的转换
- lambda参数转换为constexpr技巧,然后获取带链接的数组
- 将 Qvector<uint8_t> 转换为 QString
- 如何在cuSparse中使用cusparseXcoo2csr从coo转换为csc
- 有关插入适配器的错误。[错误]请求从 'back_insert_iterator<vector<>>' 类型转换为非标量类型
- 在c++中使用nlohmann从类到json的转换
- 从"int*"强制转换为"unsigned int"会丢失精度错误
- 将Integer转换为4字节的unsined字符矢量(按大端字节顺序)
- 处理小于cpu数据总线的数据类型.(c++转换为机器代码)
- 如何使用OpenCV将RBG图像转换为HSV,并将H、S和V值保存为C++中的3个独立图像
- 复制列表初始化的隐式转换的等级是多少
- 正在将指针转换为范围
- tf2_ros::Buffer::canTransform() 为现有转换返回 False
- 如何将 std::array 转换为 boost::asio::buffer
- C/C++ - 如何将 Buffer.BlockCopy (C#) 转换为 C/C++
- 将 vector<unsigned long> 转换为 char buffer[]
- Node.js 6增加了将Buffer.from()转换为const UInt8*的功能