您现在可以使用 boost 来运行异步非阻塞 ssl websocket 连接。检查这个例子:
https://github.com/boostorg/beast/blob/develop/example/websocket/client/async-ssl/websocket_client_async_ssl.cpp
而不是调用 ios.run(); 就像在示例中一样,使用您自己的 for 循环(来自您的网络代码)并调用 ioc.poll();
websocket 连接将在后台完全处理并将事件传递给回调类。所有事件都是异步且非阻塞的。并将与您的网络代码完美运行。还有额外的轮询调用,如 ioc.poll_for(); 轮询特定的时间。
它最终看起来像:
int main()
{
net::io_context ioc;
ssl::context ctx{ssl::context::tlsv12_client};
load_server_certificate(ctx);
// create the instance of the boost::asio websocket client
auto websock = std::make_shared<session>(ioc, ctx);
websock->run("gateway.discord.gg", "443", "");
// create a server from your own networking code;
auto server = MyLibrary->createSomeAsynchNonBlockingServer("3455", &my_librariies_event_handler);
while(MyLibrary->running)
{
if(MyLibrary->eventHandler()->proccessServer())
server->acceptClient(); // accept normal connection from your libraries server
ioc.poll(); // poll the boot::asio::io_context for any eventHandlers that have waiting events This function is non-blocking
}
}