Internet Protocol
Header Only Library

Websocket Server Openssl

How to use Websocket server class with openssl

References

#include "IP/InternetProtocol.hpp"

Syntax

namespace InternetProtocol
class TCPServerSsl

Example code

#include <iostream>
#define ASIO_USE_OPENSSL
#include <IP/Websocket/WebsocketServer.hpp>
 
using namespace InternetProtocol;
 
int main()
{
    WebsocketServerSsl server;
    server.set_socket(EProtocolType::V4, 3000);
    server.on_message_received = [&](const FWsMessage message, const ssl_socket_ptr &ssl_socket) {
        std::cout << ssl_socket->lowest_layer().remote_endpoint().port() << " -> " << buffer_to_string(message.payload) << std::endl;
    };
    server.get_ssl_context().set_options(asio::ssl::verify_none);
    server.load_private_key_file("<key file>.pem"); //Relative path to your PEM file
    server.load_certificate_file("<cert file>.pem"); //Relative path to your PEM file
    server.open();
 
    std::string input;
    std::cout << "Digite algo: ";
    while (std::getline(std::cin, input)) {
        if (input == "quit")
        {
            server.close();
            break;
        }
    }
    return 0;
}

Functions

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

Get Ssl Context

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

Get Ssl Sockets

  • Get all ssl sockets connected
  const std::set<ssl_socket_ptr> &get_ssl_sockets() const

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