Internet Protocol
Header Only Library

Websocket Client Openssl

How to use Websocket client class with openssl

References

#include "IP/InternetProtocol.hpp"

Syntax

namespace InternetProtocol
class WebsocketClientSsl
#include <iostream>
#define ASIO_USE_OPENSSL
#include <IP/Websocket/WebsocketClient.hpp>
 
using namespace InternetProtocol;
 
int main()
{
    WebsocketClientSsl client;
    client.set_host("localhost", "3000");
    client.on_message_received = [&](const FWsMessage message) {
        std::cout << message.toString() << std::endl;
    };
    client.get_ssl_context().set_options(asio::ssl::verify_none);
    client.load_private_key_file("<key file>.pem"); //Relative path to your PEM file
    client.load_certificate_file("<cert file>.pem"); //Relative path to your PEM file
    client.update_ssl_socket();
    client.connect();
 
    std::string input;
    std::cout << "Digite algo: ";
    while (std::getline(std::cin, input)) {
        if (input == "quit")
        {
            client.close();
            break;
        }
        client.send_str(input);
    }
 
    return 0;
}

Functions

This class has the same functions from WebsocketClient, except for the ones shown below

Get Ssl Context

  • Set Ssl extra configuration.
  asio::ssl::context &get_ssl_context()

Get Ssl Socket

  • Get Ssl socket
  const asio::ssl::stream<asio::ip::tcp::socket> &get_ssl_socket() const

Update Ssl Socket

  • Update Ssl socket configuration
  • Must be called afer change options using get_ssl_context() function
  void update_ssl_socket()

Load Private Key Data

  • Load the private key from a string.
  • If any error occurs, it will trigger the OnError event.
 bool load_private_key_data(const std::string &key_data)

Load Private Key File

  • Load the private key from file.
  • If any error occurs, it will trigger the OnError event.
  • Relative path.
 bool load_private_key_file(const std::string &filename)

Load Certificate Data

  • Load the certificate from a string.
  • If any error occurs, it will trigger the OnError event.
 bool load_certificate_data(const std::string &cert_data)

Load Certificate File

  • Load the certificate from file.
  • If any error occurs, it will trigger the OnError event.
  • Relative path.
 bool load_certificate_file(const std::string &filename)

Load Certificate Chain Data

  • Load the certificate chain from a string.
  • If any error occurs, it will trigger the OnError event.
 bool load_certificate_chain_data(const std::string &cert_chain_data)

Load Certificate Chain File

  • Load the certificate chain from file.
  • If any error occurs, it will trigger the OnError event.
  • Relative path.
 bool load_certificate_chain_file(const std::string &filename)

Load Verify File

  • Load the certificate from file and verify if it is valid.
  • If any error occurs, it will trigger the OnError event.
  • Relative path.
 bool load_verify_file(const std::string &filename)

On this page