Internet Protocol
Header Only Library

Websocket Server

How to use Websocket server class

References

#include "IP/InternetProtocol.hpp"

Syntax

namespace InternetProtocol
class WebsocketServer

Example Code

#include <iostream>
#include <IP/Websocket/WebsocketServer.hpp>
 
using namespace InternetProtocol;
 
int main()
{
    WebsocketServer server;
    server.set_socket(EProtocolType::V4, 3000);
    server.on_message_received = [&](const FWsMessage message, const socket_ptr &socket) {
        std::cout << socket->remote_endpoint().port() << " -> " << buffer_to_string(message.payload) << std::endl;
    };
    server.open();
 
    std::string input;
    std::cout << "Digite algo: ";
    while (std::getline(std::cin, input)) {
        if (input == "quit")
        {
            server.close();
            break;
        }
    }
    return 0;
}

Variables

EOpcode

namespace InternetProtocol
 
enum class EOpcode : uint8_t {
    FRAME_CON,
    TEXT_FRAME,
    BINARY_FRAME,
    NON_CONTROL_FRAMES,
    CONNECTION_CLOSE,
    PING,
    PONG,
    FURTHER_FRAMES,
};

FDataFrame

namespace InternetProtocol
 
struct FDataFrame {
    bool fin;
    bool rsv1;
    bool rsv2;
    bool rsv3;
    bool mask;
    EOpcode opcode;
    size_t length;
    std::array<std::byte, 4> masking_key;
};

FWsMessage

namespace InternetProtocol
 
struct FWsMessage {
    FDataFrame data_frame;
    std::vector<std::byte> payload;
    std::string toString() const;
    size_t size;
};

FRequest

namespace InternetProtocol::Server
 
struct FRequest {
    EMethod method = EMethod::GET;
    std::string path = "/";
    std::string version = "1.1";
    std::map<std::string, std::vector<std::string> > headers;
    std::string body;
};

FResponse

namespace InternetProtocol::Server
 
struct FResponse {
    std::string version = "1.1";
    std::map<std::string, std::string> headers;
    std::string body;
};

Functions

Set Socket

  • Set the adress, port, max connection of socket acceptor
void set_socket(const EProtocolType protocol, const uint16_t port, const int max_listen_conn = 2147483647)

Get Acceptor

  • Get socket acceptor
asio::ip::tcp::acceptor &get_acceptor()

Get Sockets

  • Get all sockets connected to the server
const std::set<socket_ptr> get_sockets() const

Set Max Send Buffer Size

  • Set max size in bytes of send buffer
void set_max_send_buffer_size(int value = 1400)

Get Max Send Buffer Size

  • Get max size in bytes of send buffer
int get_max_send_buffer_size() const

Set Split Package

  • If the size of a message exceeds the maximum allowed size for the send buffer, it will be divided into smaller packets before being sent.
void set_split_package(bool value = true)

Get Split Package

  • Get if Split Package is enabled
bool get_split_package() const

Send Handshake To

  • Send a websocket handshake to stablish connection with
  • Return false if socket is closed
  • Warning: If you’re using Windows, ensure that you are sending characters compatible with UTF-8
bool send_handshake_to(const Server::FRequest &request, Server::FResponse &response, const socket_ptr &socket)

Send Handshake Error To

  • Send a websocket handshake error and close connection
  • Return false if socket is closed
  • Warning: If you’re using Windows, ensure that you are sending characters compatible with UTF-8
bool send_handshake_error_to(const uint32_t status_code, const std::string &body, const socket_ptr &socket)

Send Str To

  • Send a string message
  • Return false if string is empty or socket is closed
  • Warning: If you’re using Windows, ensure that you are sending characters compatible with UTF-8
bool send_str_to(const std::string &message, const socket_ptr &socket)

Send Buffer To

  • Send a buffer message
  • Return false if byte array is empty or socket is closed
bool send_buffer_to(const std::vector<uint8_t> &buffer, const socket_ptr &socket)

Send Ping To

  • Send a ping to websocket server
  • Return false if socket is closed
bool send_ping_to(const socket_ptr &socket)

Open

  • Open connection
  • Return false if socket is already open or any error occurs
  • Event on_error is triggered if any error occurs
bool open()

Close

  • Disconnect all clients and close connection
void close()

Disconnect Socket

  • Close connection of specific socket
void disconnect_socket(const socket_ptr &socket)

Get Error Code

  • Get latest error code
asio::error_code get_error_code() const

Handshake Functions

Handshake is a http request to contact the server and requesting a WebSocket connection

Set Headers

  • Set handshake headers
void set_headers(const std::map<std::string, std::string> &headers)

Get Headers

  • Return a reference to handshake headers
  • Use this function to modify or append values to headers
std::map<std::string, std::string> &get_headers()

Dataframe Functions

Set RSV1

void set_RSV1(bool value = false)

Use RSV1

bool use_RSV1() const

Set RSV2

void set_RSV2(bool value = false)

Use RSV2

bool use_RSV2() const

Set RSV3

void set_RSV3(bool value = false)

Use RSV3

bool use_RSV3() const

Set Mask

void set_Mask(bool value = true)

Use Mask

bool use_Mask() const

Events

On Socket Accepted

  • Event triggered when a new client connection is estabilished
std::function<void(const socket_ptr &)> on_socket_accepted

On Bytes Transferred

  • Event triggered when socket send or receive data
  • size_t: bytes sent
  • size_t: bytes received
std::function<void(const size_t, const size_t)> on_bytes_transfered

On Message Sent

  • Event triggered when a message is sent by specific socket
  • const asio::error_code &: if error occur, error code will be different from 0
std::function<void(const asio::error_code &, const socket_ptr &)> on_message_sent

On Message Received

  • Event triggered when a message is received by specific socket
  • FWsMessage: message struct
  • Use buffer_to_string() function to converte raw_data to std::string
std::function<void(const FWsMessage, const socket_ptr &)> on_message_received

On Pong Received

  • Event triggered when a pong message is received
std::function<void(const socket_ptr &)> on_pong_received

On Close Notify

  • Event triggered when a client send a close notification
std::function<void(const socket_ptr &)> on_close_notify

On Close

  • Event triggered when a connection is closed
std::function<void()> on_close

On Error

  • Event triggered if any error occur during async process.
  • const asio::error_code &: if error occur, error code will be different from 0.
  • Normally, when an error occurs, the socket is automatically closed, triggering the on_close event.
std::function<void(const asio::error_code &)> on_error