Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/cplusplus/156.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C++ C++;Asio连接到wss_C++_Websocket_Asio - Fatal编程技术网

C++ C++;Asio连接到wss

C++ C++;Asio连接到wss,c++,websocket,asio,C++,Websocket,Asio,我正在尝试使用asio连接到安全的websocket 此示例适用于ip地址: #include <iostream> #include <asio.hpp> int main() { asio::error_code ec; asio::io_context context; asio::io_context::work idleWork(context); asio::ip::tcp::endpoint endpoint(asio:

我正在尝试使用asio连接到安全的websocket

此示例适用于ip地址:

#include <iostream>
#include <asio.hpp>
int main() {
    asio::error_code ec;

    asio::io_context context;

    asio::io_context::work idleWork(context);

    asio::ip::tcp::endpoint endpoint(asio::ip::make_address("51.38.81.49", ec), 80);

    asio::ip::tcp::socket socket(context);

    socket.connect(endpoint, ec);

    if (!ec) {
        std::cout << "Connected!" << std::endl;
    } else {
        std::cout << "Failed to connect to address: \n" << ec.message() << std::endl;
    }
return 0;
}
#包括
#包括
int main(){
asio::错误代码ec;
asio::io_上下文;
asio::io_上下文::空闲工作(上下文);
asio::ip::tcp::endpoint端点(asio::ip::make_地址(“51.38.81.49”,ec),80);
asio::ip::tcp::套接字(上下文);
插座连接(端点,ec);
如果(!ec){
std::cout为此,请使用类:

#include <asio.hpp>
#include <asio/ts/buffer.hpp>

std::vector<char> vBuffer(1 * 1024);

// This should output the received data
void GrabSomeData(asio::ip::tcp::socket& socket) {
    socket.async_read_some(asio::buffer(vBuffer.data(), vBuffer.size()),
        [&](std::error_code ec, std::size_t lenght) {
            if (!ec) {
                std::cout << "\n\nRead " << lenght << " bytes\n\n";

                for (int i = 0; i < lenght; i++)
                    std::cout << vBuffer[i];

                GrabSomeData(socket);
            }
        }
    );
}

int main() {

    asio::error_code ec;

    asio::io_context context;

    asio::io_context::work idleWork(context);

    std::thread thrContext = std::thread([&]() { context.run(); });
    
    // I hope this is what you meant
    asio::ip::tcp::resolver res(context);
    asio::ip::tcp::socket socket(context);
    asio::connect(socket, res.resolve("echo.websocket.org", std::to_string(443)));

    // Check the socket is open
    if (socket.is_open()) {
        // Start to output incoming data
        GrabSomeData(socket);

        // Send data to the websocket, which should be sent back
        std::string sRequest = "Echo";
        socket.write_some(asio::buffer(sRequest.data(), sRequest.size()), ec);
        
        // Wait some time, so the data is received
        using namespace std::chrono_literals;
        std::this_thread::sleep_for(20000ms);

        context.stop();
        if (thrContext.joinable()) thrContext.join();
    }

    return 0;
}
    tcp::resolver res(context);
    asio::ip::tcp::socket socket(context);
    boost::asio::connect(socket, res.resolve("api2.example.com", 80));