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

Client-side WebSocket framing layer over an already-connected socket. More...

#include <ws.hpp>

Public Member Functions

 TWebSocket (TSocket &socket)
 Wraps an already-connected socket with WebSocket framing.
 
TFuture< void > Connect (const std::string &host, const std::string &path)
 Performs the HTTP → WebSocket upgrade handshake.
 
TFuture< void > SendText (std::string_view message)
 Sends message as a masked WebSocket text frame (opcode 0x1).
 
TFuture< std::string_view > ReceiveText ()
 Receives the next WebSocket text frame.
 

Detailed Description

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

Client-side WebSocket framing layer over an already-connected socket.

Wraps TSocket with RFC 6455 framing. The underlying socket must be TCP-connected before calling Connect(). TWebSocket holds a reference to the socket — the socket must outlive the TWebSocket instance.

Only text frames are exposed. Outgoing frames are client-masked as required by RFC 6455. Server-side upgrade is not included.

Template Parameters
TSocketConnected socket type providing ReadSome / WriteSome.
template<typename TPoller>
TFuture<void> chat(TPoller& poller, TAddress addr) {
typename TPoller::TSocket sock(poller, addr.Domain());
co_await sock.Connect(addr);
TWebSocket ws(sock);
co_await ws.Connect("example.com", "/chat"); // HTTP upgrade
co_await ws.SendText("hello");
std::string_view msg = co_await ws.ReceiveText(); // valid until next ReceiveText()
}
A class representing an IPv4 or IPv6 address (with port).
Definition address.hpp:38
int Domain() const
Gets the domain (address family) of the stored address.
Definition address.cpp:45
Client-side WebSocket framing layer over an already-connected socket.
Definition ws.hpp:79
Owned coroutine handle that carries a result of type T.
Definition corochain.hpp:185

Constructor & Destructor Documentation

◆ TWebSocket()

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

Wraps an already-connected socket with WebSocket framing.

Parameters
socketReference to the connected socket. Must outlive this object.

Member Function Documentation

◆ Connect()

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

Performs the HTTP → WebSocket upgrade handshake.

Sends an HTTP/1.1 GET request with Upgrade: websocket headers, then reads and validates the server's 101 Switching Protocols response including the Sec-WebSocket-Accept header. Must be called once before SendText / ReceiveText.

Parameters
hostSent in the Host: header (e.g. "example.com").
pathRequest path (e.g. "/chat").
Exceptions
std::runtime_errorif the server rejects the upgrade or the accept key does not match.

◆ ReceiveText()

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

Receives the next WebSocket text frame.

Suspends until a complete frame arrives. The returned string_view points into an internal buffer and is valid only until the next call to ReceiveText(). Copy the contents if you need to keep them longer.

Returns
string_view over the frame payload.
Exceptions
std::runtime_errorif a non-text frame (opcode ≠ 0x1) is received.

◆ SendText()

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

Sends message as a masked WebSocket text frame (opcode 0x1).

Parameters
messageUTF-8 text to send. The caller retains ownership.

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