COROIO: NNet::TWebSocket< TSocket > Class Template Reference
COROIO
 
Loading...
Searching...
No Matches
NNet::TWebSocket< TSocket > Class Template Reference

Implements a WebSocket protocol layer on top of a given socket. More...

#include <ws.hpp>

Public Member Functions

 TWebSocket (TSocket &socket)
 Constructs a WebSocket instance wrapping the provided socket.
 
TFuture< void > Connect (const std::string &host, const std::string &path)
 Initiates the WebSocket handshake.
 
TFuture< void > SendText (std::string_view message)
 Sends a text message as a WebSocket frame.
 
TFuture< std::string_view > ReceiveText ()
 Receives a text message from the WebSocket.
 

Detailed Description

template<typename TSocket>
class NNet::TWebSocket< TSocket >

Implements a WebSocket protocol layer on top of a given socket.

The TWebSocket class wraps an underlying socket (of type TSocket) to implement the WebSocket handshake, sending, and receiving of text frames. It uses asynchronous operations (via TFuture) for I/O, and it internally maintains a reader and writer.

Overview

Template Parameters
TSocketThe underlying socket type used for network communication.

Example: Minimal WebSocket Client

#include <iostream>
#include <coroio/ws.hpp>
template<typename TSocket>
TFuture<void> WebSocketClientExample(TSocket& socket) {
TWebSocket<TSocket> ws(socket);
co_await ws.Connect("echo.websocket.org", "/.ws");
std::string message = "Hello, WebSocket!";
co_await ws.SendText(message);
std::string_view reply = co_await ws.ReceiveText();
std::cout << "Received: " << reply << std::endl;
co_return;
}
High-level asynchronous socket for network communication.
Definition socket.hpp:364
TWebSocket(TSocket &socket)
Constructs a WebSocket instance wrapping the provided socket.
Definition ws.hpp:68
Future type for coroutines returning a value of type T.
Definition corochain.hpp:177

Constructor & Destructor Documentation

◆ TWebSocket()

template<typename TSocket>
NNet::TWebSocket< TSocket >::TWebSocket ( TSocket & socket)
inlineexplicit

Constructs a WebSocket instance wrapping the provided socket.

Parameters
socketThe underlying socket that will be used for the WebSocket connection.

Member Function Documentation

◆ Connect()

template<typename TSocket>
TFuture< void > NNet::TWebSocket< TSocket >::Connect ( const std::string & host,
const std::string & path )
inline

Initiates the WebSocket handshake.

Constructs and sends an HTTP GET request with the proper headers including a generated WebSocket key, then waits for the server's response. The response is verified to contain the "101 Switching Protocols" status and a valid Sec-WebSocket-Accept header.

Parameters
hostThe target host name.
pathThe resource path.
Returns
A TFuture that completes when the handshake is successful.
Exceptions
std::runtime_errorif the handshake fails.

◆ ReceiveText()

template<typename TSocket>
TFuture< std::string_view > NNet::TWebSocket< TSocket >::ReceiveText ( )
inline

Receives a text message from the WebSocket.

Waits for an incoming frame, validates that it is a text frame, and returns its payload.

Returns
A TFuture that yields a string_view containing the text message.
Exceptions
std::runtime_errorif a non-text frame is received.

◆ SendText()

template<typename TSocket>
TFuture< void > NNet::TWebSocket< TSocket >::SendText ( std::string_view message)
inline

Sends a text message as a WebSocket frame.

Parameters
messageThe text message to send.
Returns
A TFuture that completes when the message has been sent.

The documentation for this class was generated from the following file: