Internet Protocol
Header Only Library

Websocket Client

How to use Websocket client class

References

#include "ip.hpp"
#include "ip/websocket/wsclient.hpp"

Syntax

namespace internetprotocol
class ws_client_c

Example Code

#include <iostream>
#include <ip/websocket/wsclient.hpp>

using namespace InternetProtocol;

int main()
{
    ws_client_c net;

    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;
}

Variables

Handshake

  • Set/Get handshake headers.
http_request_t handshake

Functions

Is Open

  • Return true if socket is open.
bool is_open() const

Local Endpoint

  • Get the local endpoint of the socket. Use this function only after open connection.
tcp::endpoint local_endpoint() const

Remote Endpoint

  • Get the remote endpoint of the socket. Use this function only after open connection.
tcp::endpoint remote_endpoint() const

Get Error Code

  • Return a const ref of the latest error code returned by asio.
const asio::error_code &get_error_code() const

Write

  • 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 write(const std::string &message, const dataframe_t &dataframe = {}, const std::function<void(const asio::error_code &, const size_t)> &callback = nullptr)

Write Buffer

  • Send a buffer message
  • Return false if byte array is empty or socket is closed
bool write_buffer(const std::vector<uint8_t> &buffer, const dataframe_t &dataframe = {}, const std::function<void(const asio::error_code &, const size_t)> &callback = nullptr)

Ping

  • Send a ping to websocket server
  • Return false if socket is closed
bool ping(const std::function<void(const asio::error_code &, const size_t)> &callback = nullptr)

Pong

  • Send a pong to websocket server
  • Return false if socket is closed
bool pong(const std::function<void(const asio::error_code &, const size_t)> &callback = nullptr)

Connect

  • Connect to the host
  • Return true if It's able to connect
bool connect(const client_bind_options_t &bind_opts = {})

End

  • Perform a smooth close of socket following ws protocol and stop listening for data on it. 'on_close' event will be triggered.
void end(const uint16_t code = 1000, const std::string &reason = "")

Close

  • Close connection
void close(const uint16_t code = 1000, const std::string &reason = "")

Events

On Connected

  • Event triggered when a connection is estabilished
std::function<void()> on_connected

On Unexpected Handshake

  • This event will be triggered when a bad handshake has been received from server.
std::function<void(const http_response_t &)> on_unexpected_handshake

On Message Received

  • This event will be triggered when a message has been received.
std::function<void(const std::vector<uint8_t> &, bool)> on_message_received

On Ping

  • This event will be triggered when a ping has been received.
std::function<void()> on_ping;

On Pong

  • This event will be triggered when a pong has been received.
std::function<void()> on_pong
```unction<void()> on_close_notify

On Close

  • Event triggered when connection is closed
std::function<void(const uint16_t, const std::string &)> 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.
std::function<void(const asio::error_code &)> on_error