Websocket 客户端在 C++ 中使用 boost::Beast - 在写入操作时抛出错误
Websocket client in C++ using boost::Beast - throwing error at write operation
下面是一段我正在努力使其成功的代码。我使用 git hub https://www.boost.org/doc/libs/1_70_0/libs/beast/doc/html/beast/quick_start/websocket_client.html 上提供的参考代码。 问题是当我调用连接方法并在同一个函数中进行写入操作时,它可以工作,但是如果我将其放在不同的函数中,它将不起作用。
我是新手,可以提升和shared_pointer,如果我没有任何意义,请原谅。
// Sends a WebSocket message and prints the response
class CWebSocket_Sync : public std::enable_shared_from_this<CWebSocket_Sync>
{
tcp::resolver resolver_;
websocket::stream<tcp::socket> ws_;
boost::beast::multi_buffer buffer_;
std::string host_;
std::string text_;
public:
// Resolver and socket require an io_context
explicit
CWebSocket_Sync(boost::asio::io_context& ioc)
: resolver_(ioc)
, ws_(ioc)
{
}
void
connect(
char const* host,
char const* port,
char const* text)
{
// Save these for later
host_ = host;
text_ = text;
// Look up the domain name
auto const results = resolver_.resolve(host, port);
// Make the connection on the IP address we get from a lookup
auto ep = net::connect(ws_.next_layer(), results);
// Update the host_ string. This will provide the value of the
// Host HTTP header during the WebSocket handshake.
// See https://tools.ietf.org/html/rfc7230#section-5.4
host_ += ':' + std::to_string(ep.port());
// Perform the websocket handshake
ws_.handshake(host_, "/");
//ws_.write(net::buffer(std::string(text)));
//// This buffer will hold the incoming message
//beast::flat_buffer buffer;
//// Read a message into our buffer
//ws_.read(buffer);
}
void ServerCommand(char const* text)
{
ws_.write(net::buffer(std::string(text))); // <-- this line throw memory error
// This buffer will hold the incoming message
beast::flat_buffer buffer;
// Read a message into our buffer
ws_.read(buffer);
// The make_printable() function helps print a ConstBufferSequence
std::cout << beast::make_printable(buffer.data()) << std::endl;
}
void CloseConnection()
{
// Close the WebSocket connection
ws_.close(websocket::close_code::normal);
}
};
int main(int argc, char** argv)
{
auto const host = "127.0.0.1";
auto const port = "7011";
auto const loginCmd = "login"
boost::asio::io_context ioc;
std::make_shared<CWebSocket_Sync>(ioc)->connect(host, port, loginCmd);
std::make_shared<CWebSocket_Sync>(ioc)->ServerCommand(loginCmd);
std::make_shared<CWebSocket_Sync>(ioc)->CloseConnection();
return EXIT_SUCCESS;
}
std::make_shared<CWebSocket_Sync>(ioc)->connect(host, port);
std::make_shared<CWebSocket_Sync>(ioc)->ServerCommand(loginCmd);
std::make_shared<CWebSocket_Sync>(ioc)->CloseConnection();
每行创建一个新客户端(make_shared<CWebSocket_Sync>
(,并在其上运行一个步骤。您可能想要什么:
auto client = std::make_shared<CWebSocket_Sync>(ioc);
client->connect(host, port);
client->ServerCommand(loginCmd);
client->CloseConnection();
事实上,这是有效的:
#include <boost/beast.hpp>
#include <boost/beast/websocket.hpp>
#include <memory>
#include <iostream>
namespace net = boost::asio;
namespace beast = boost::beast;
namespace websocket = beast::websocket;
using net::ip::tcp;
// Sends a WebSocket message and prints the response
class CWebSocket_Sync : public std::enable_shared_from_this<CWebSocket_Sync> {
tcp::resolver resolver_;
websocket::stream<tcp::socket> ws_;
boost::beast::multi_buffer buffer_;
std::string host_;
public:
// Resolver and socket require an io_context
explicit CWebSocket_Sync(boost::asio::io_context& ioc)
: resolver_(ioc), ws_(ioc) { }
void connect(char const* host, char const* port) {
// Save these for later
host_ = host;
// Look up the domain name
auto const results = resolver_.resolve(host, port);
// Make the connection on the IP address we get from a lookup
auto ep = net::connect(ws_.next_layer(), results);
// Update the host_ string. This will provide the value of the
// Host HTTP header during the WebSocket handshake.
// See https://tools.ietf.org/html/rfc7230#section-5.4
host_ += ':' + std::to_string(ep.port());
// Perform the websocket handshake
ws_.handshake(host_, "/");
}
void ServerCommand(char const* text) {
ws_.write(net::buffer(std::string(text))); // <-- this line throw memory error
beast::flat_buffer buffer;
ws_.read(buffer);
std::cout << beast::make_printable(buffer.data()) << std::endl;
}
void CloseConnection() {
ws_.close(websocket::close_code::normal);
}
};
int main() {
auto const host = "127.0.0.1";
auto const port = "7011";
auto const loginCmd = "login";
boost::asio::io_context ioc;
auto client = std::make_shared<CWebSocket_Sync>(ioc);
client->connect(host, port);
client->ServerCommand(loginCmd);
client->CloseConnection();
}
简化
但是,由于没有任何东西使用共享生存期,并且您没有在任何地方使用异步调用,为什么不简单地:
住在科里鲁
class CWebSocket_Sync {
后来:
CWebSocket_Sync client(ioc);
client.connect(host, port);
client.ServerCommand(loginCmd);
client.CloseConnection();
相关文章:
- 给定一个整数数组,需要在Max_Heap上运行操作。得到错误"segmentation fault",有什么想法吗?(C++)
- C++ 随机数生成器:尝试将结果作为向量获取,但通过制作 void 函数来执行此操作而出现错误
- 如何修复无效的API密钥,IP或操作权限错误?
- 使用 OpenCV readNetFromTensorFlow 运行 Keras DNN 模型 (UNet):错误:操作
- Websocket 客户端在 C++ 中使用 boost::Beast - 在写入操作时抛出错误
- 在C++中使用友元函数操作模板类中的私有变量时会发生错误
- 如何修复重新平衡时AVL删除操作中的分段错误?
- 元数据操作失败LNK2022错误 (8013118D):重复类型中的布局信息不一致 (选择设备参数):(0x020002
- 我的移动分配操作程序重载的分段错误
- 打印矩阵后分割错误,但在打印额外行后修复(ostream <<操作器)
- glteximage3d抛出错误1282(无效操作)
- INC 操作码编译到错误的地址
- 行为:内存操作错误或内存泄漏C++
- 如何在OpenGL中使用纹理?(无效操作错误:1282)
- 表操作错误
- std::C++生成器 6 上的字符串操作错误
- Visual Studio 中的 Cin 操作错误
- 简单的数组操作错误
- c++中矢量操作错误
- 内存操作错误