Header Only Library
Websocket Server Openssl
How to use Websocket server class with openssl
References
#include "ip.hpp"
#include "ip/websocket/wsserver.hpp"
#define ENABLE_SSL
Syntax
namespace internetprotocol
class ws_server_ssl_c
Example code
#include <iostream>
#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_server_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_client_accepted = [&](const std::shared_ptr<ws_remote_c> &client) {
std::cout << "Client connected" << std::endl;
client->on_error = [&](const asio::error_code &ec) {
std::cout << ec.message() << std::endl;
};
client->on_close = [&](const uint16_t code, const std::string &reason) {
std::cout << code << " -> " << reason << std::endl;
};
client->on_message_received = [&](const std::vector<uint8_t> &buffer, bool is_binary) {
std::cout << "Message received: " << buffer_to_string(buffer) << std::endl;
};
};
net.open();
std::string input;
while (std::getline(std::cin, input)) {
if (input == "quit") {
net.close();
break;
}
for (const auto &client : net.clients()) {
client->write(input, {}, [](const asio::error_code &ec, const size_t bytes_sent) {
// Check if message has been sent
});
}
}
join_threads();
return 0;
}