Network System 0.1.1
High-performance modular networking library for scalable client-server applications
Loading...
Searching...
No Matches
kcenon::network::protocol::websocket Namespace Reference

Functions

auto create_connection (std::string_view id="") -> std::unique_ptr< unified::i_connection >
 Creates a WebSocket connection (not yet started)
 
auto connect (std::string_view url, std::string_view id="") -> std::unique_ptr< unified::i_connection >
 Creates and starts a WebSocket connection in one call.
 
auto connect (const unified::endpoint_info &endpoint, std::string_view path="/", std::string_view id="") -> std::unique_ptr< unified::i_connection >
 Creates and starts a WebSocket connection using endpoint info.
 
auto create_listener (std::string_view id="") -> std::unique_ptr< unified::i_listener >
 Creates a WebSocket listener (not yet listening)
 
auto listen (const unified::endpoint_info &bind_address, std::string_view path="/", std::string_view id="") -> std::unique_ptr< unified::i_listener >
 Creates and starts a WebSocket listener in one call.
 
auto listen (uint16_t port, std::string_view path="/", std::string_view id="") -> std::unique_ptr< unified::i_listener >
 Creates and starts a WebSocket listener on a specific port.
 

Function Documentation

◆ connect() [1/2]

auto kcenon::network::protocol::websocket::connect ( const unified::endpoint_info & endpoint,
std::string_view path = "/",
std::string_view id = "" ) -> std::unique_ptr<unified::i_connection>
nodiscard

Creates and starts a WebSocket connection using endpoint info.

Parameters
endpointThe endpoint with host and port
pathThe WebSocket path (default: "/")
idOptional unique identifier for the connection
Returns
Unique pointer to an i_connection instance (connecting)

This overload is useful when host and port are known separately. The connection will use plain WebSocket (ws://).

Usage Example

auto conn = protocol::websocket::connect({"localhost", 8080}, "/ws");
auto connect(std::string_view url, std::string_view id="") -> std::unique_ptr< unified::i_connection >
Creates and starts a WebSocket connection in one call.
Definition websocket.cpp:39

Definition at line 47 of file websocket.cpp.

51{
52 std::string connection_id = id.empty() ? generate_unique_id("ws-conn") : std::string(id);
53 auto adapter = std::make_unique<unified::adapters::ws_connection_adapter>(connection_id);
54 adapter->set_path(path);
55 (void)adapter->connect(endpoint);
56 return adapter;
57}

◆ connect() [2/2]

auto kcenon::network::protocol::websocket::connect ( std::string_view url,
std::string_view id = "" ) -> std::unique_ptr<unified::i_connection>
nodiscard

Creates and starts a WebSocket connection in one call.

Parameters
urlThe WebSocket URL to connect to (ws:// or wss://)
idOptional unique identifier for the connection
Returns
Unique pointer to an i_connection instance (connecting)

This is a convenience function that creates a WebSocket connection and immediately initiates the connection with the specified URL.

URL Format

  • ws://host:port/path - Plain WebSocket
  • wss://host:port/path - Secure WebSocket (TLS)
  • Port defaults to 80 for ws:// and 443 for wss:// if not specified

Usage Example

auto conn = protocol::websocket::connect("ws://localhost:8080/ws");
conn->set_callbacks({
.on_data = [](std::span<const std::byte> data) { }
});
// Connection is initiating...

Definition at line 39 of file websocket.cpp.

41{
42 auto conn = create_connection(id);
43 (void)conn->connect(url);
44 return conn;
45}

References create_connection().

Here is the call graph for this function:

◆ create_connection()

auto kcenon::network::protocol::websocket::create_connection ( std::string_view id = "") -> std::unique_ptr<unified::i_connection>
nodiscard

Creates a WebSocket connection (not yet started)

Parameters
idOptional unique identifier for the connection
Returns
Unique pointer to an i_connection instance

The returned connection is not started. Call connect() with a URL to establish the WebSocket connection.

WebSocket Semantics

WebSocket is a full-duplex protocol over TCP. The connection starts with an HTTP upgrade handshake.

  • connect() accepts URLs like "ws://host:port/path" or "wss://host:port/path"
  • is_connected() returns true after successful handshake
  • send() sends binary frames (use underlying client for text)

Usage Example

auto conn = protocol::websocket::create_connection("my-ws-client");
conn->set_callbacks({
.on_connected = []() { std::cout << "WebSocket connected!\n"; },
.on_data = [](std::span<const std::byte> data) {
// Handle received WebSocket message
}
});
conn->connect("ws://localhost:8080/ws");
auto create_connection(std::string_view id="") -> std::unique_ptr< unified::i_connection >
Creates a WebSocket connection (not yet started)
Definition websocket.cpp:33

Definition at line 33 of file websocket.cpp.

34{
35 std::string connection_id = id.empty() ? generate_unique_id("ws-conn") : std::string(id);
36 return std::make_unique<unified::adapters::ws_connection_adapter>(connection_id);
37}

Referenced by connect().

Here is the caller graph for this function:

◆ create_listener()

auto kcenon::network::protocol::websocket::create_listener ( std::string_view id = "") -> std::unique_ptr<unified::i_listener>
nodiscard

Creates a WebSocket listener (not yet listening)

Parameters
idOptional unique identifier for the listener
Returns
Unique pointer to an i_listener instance

The returned listener is not listening. Call start() to begin accepting WebSocket connections.

WebSocket Server Semantics

WebSocket servers accept HTTP upgrade requests and establish full-duplex connections with clients.

  • on_accept is called when a WebSocket handshake completes
  • connection_id uniquely identifies each connected client
  • send_to() sends data to specific clients
  • broadcast() sends data to all connected clients

Usage Example

auto listener = protocol::websocket::create_listener("my-ws-server");
listener->set_callbacks({
.on_accept = [](std::string_view conn_id) {
std::cout << "New WebSocket client: " << conn_id << "\n";
},
.on_data = [](std::string_view conn_id, std::span<const std::byte> data) {
// Handle received message from conn_id
}
});
listener->start(8080);
auto create_listener(std::string_view id="") -> std::unique_ptr< unified::i_listener >
Creates a WebSocket listener (not yet listening)
Definition websocket.cpp:59

Definition at line 59 of file websocket.cpp.

60{
61 std::string listener_id = id.empty() ? generate_unique_id("ws-listener") : std::string(id);
62 return std::make_unique<unified::adapters::ws_listener_adapter>(listener_id);
63}

◆ listen() [1/2]

auto kcenon::network::protocol::websocket::listen ( const unified::endpoint_info & bind_address,
std::string_view path = "/",
std::string_view id = "" ) -> std::unique_ptr<unified::i_listener>
nodiscard

Creates and starts a WebSocket listener in one call.

Parameters
bind_addressThe local address to bind to
pathThe WebSocket path to handle (default: "/")
idOptional unique identifier for the listener
Returns
Unique pointer to an i_listener instance (listening)

This is a convenience function that creates a listener and immediately starts listening on the specified address.

Usage Example

auto listener = protocol::websocket::listen({"0.0.0.0", 8080}, "/ws");
listener->set_callbacks({
.on_data = [](std::string_view conn_id, std::span<const std::byte> data) { }
});
// Listener is already accepting connections
auto listen(const unified::endpoint_info &bind_address, std::string_view path="/", std::string_view id="") -> std::unique_ptr< unified::i_listener >
Creates and starts a WebSocket listener in one call.
Definition websocket.cpp:65

Definition at line 65 of file websocket.cpp.

69{
70 std::string listener_id = id.empty() ? generate_unique_id("ws-listener") : std::string(id);
71 auto adapter = std::make_unique<unified::adapters::ws_listener_adapter>(listener_id);
72 adapter->set_path(path);
73 (void)adapter->start(bind_address);
74 return adapter;
75}

Referenced by listen().

Here is the caller graph for this function:

◆ listen() [2/2]

auto kcenon::network::protocol::websocket::listen ( uint16_t port,
std::string_view path = "/",
std::string_view id = "" ) -> std::unique_ptr<unified::i_listener>
nodiscard

Creates and starts a WebSocket listener on a specific port.

Parameters
portThe port number to listen on
pathThe WebSocket path to handle (default: "/")
idOptional unique identifier for the listener
Returns
Unique pointer to an i_listener instance (listening)

Convenience overload that binds to all interfaces (0.0.0.0).

Definition at line 77 of file websocket.cpp.

79{
80 return listen(unified::endpoint_info{"0.0.0.0", port}, path, id);
81}
Network endpoint information (host/port or URL)
Definition types.h:56

References listen().

Here is the call graph for this function: