Internet Protocol
Header Only Library

Websocket Client Openssl

How to use Websocket client class with openssl

References

#include "ip.hpp"
#include "ip/websocket/wesclient.hpp"
#define ENABLE_SSL

Syntax

namespace internetprotocol
class ws_client_ssl_c
#include <iostream>
#include <fstream>
#define ENABLE_SSL
#include <ip.hpp>

using namespace internetprotocol;

std::string loadfile(const std::string& file) {
    std::ifstream arquivo(file);

    if (!arquivo.is_open()) {
        throw std::runtime_error("Erro trying to open file: " + file);
    }

    std::stringstream buffer;
    buffer << arquivo.rdbuf();

    return buffer.str();
}
// Relative path to the files
const std::string cert = loadfile("cert.pem");
const std::string key = loadfile("key.pem");
const std::string csr = loadfile("csr.pem");
const std::string ca_cert = loadfile("ca-cert.pem");

int main()
{
    ws_client_ssl_c net({ key, cert, "", "", file_format_e::pem, none, "" });
    
    net.on_error = [&](const asio::error_code &ec) {
        std::cout << ec.message() << std::endl;
    };
    net.on_message_received = [&](const std::vector<uint8_t> &buffer, bool is_binary) {
        std::cout << buffer_to_string(buffer) << std::endl;
    };
    net.connect();

    std::string input;
    while (std::getline(std::cin, input)) {
        if (input == "quit") {
            net.close(1000, "Shutdown client");
            break;
        }
        net.write(input, {}, [](const asio::error_code &ec, size_t bytes_sent) {
            // Check if message has been sent
        });
    }
    join_threads();

    return 0;
}

On this page