WPILibC++ 2024.1.1-beta-4
wpi::HttpWebSocketServerConnection< Derived > Class Template Referenceabstract

A server-side HTTP connection that also accepts WebSocket upgrades. More...

#include <wpinet/HttpWebSocketServerConnection.h>

Inheritance diagram for wpi::HttpWebSocketServerConnection< Derived >:
wpi::HttpServerConnection

Public Member Functions

 HttpWebSocketServerConnection (std::shared_ptr< uv::Stream > stream, std::span< const std::string_view > protocols)
 Constructor. More...
 
 HttpWebSocketServerConnection (std::shared_ptr< uv::Stream > stream, std::initializer_list< std::string_view > protocols)
 Constructor. More...
 
- Public Member Functions inherited from wpi::HttpServerConnection
 HttpServerConnection (std::shared_ptr< uv::Stream > stream)
 
virtual ~HttpServerConnection ()=default
 

Protected Member Functions

virtual bool IsValidWsUpgrade (std::string_view protocol)
 Check that an incoming WebSocket upgrade is okay. More...
 
virtual void ProcessWsUpgrade ()=0
 Process an incoming WebSocket upgrade. More...
 
- Protected Member Functions inherited from wpi::HttpServerConnection
virtual void ProcessRequest ()=0
 Process an incoming HTTP request. More...
 
virtual void BuildCommonHeaders (raw_ostream &os)
 Build common response headers. More...
 
virtual void BuildHeader (raw_ostream &os, int code, std::string_view codeText, std::string_view contentType, uint64_t contentLength, std::string_view extra={})
 Build HTTP response header, along with other header information like mimetype. More...
 
void SendData (std::span< const uv::Buffer > bufs, bool closeAfter=false)
 Send data to client. More...
 
virtual void SendResponse (int code, std::string_view codeText, std::string_view contentType, std::string_view content, std::string_view extraHeader={})
 Send HTTP response, along with other header information like mimetype. More...
 
virtual void SendStaticResponse (int code, std::string_view codeText, std::string_view contentType, std::string_view content, bool gzipped, std::string_view extraHeader={})
 Send HTTP response from static data, along with other header information like mimetype. More...
 
virtual void SendError (int code, std::string_view message={})
 Send error header and message. More...
 

Protected Attributes

WebSocketm_websocket = nullptr
 WebSocket connection; not valid until ProcessWsUpgrade is called. More...
 
- Protected Attributes inherited from wpi::HttpServerConnection
HttpParser m_request {HttpParser::kRequest}
 The HTTP request. More...
 
bool m_keepAlive = false
 Whether the connection should be kept alive. More...
 
bool m_acceptGzip = false
 If gzip is an acceptable encoding for responses. More...
 
uv::Streamm_stream
 The underlying stream for the connection. More...
 
sig::ScopedConnection m_dataConn
 The header reader connection. More...
 
sig::ScopedConnection m_endConn
 The end stream connection. More...
 
sig::Connection m_messageCompleteConn
 The message complete connection. More...
 

Detailed Description

template<typename Derived>
class wpi::HttpWebSocketServerConnection< Derived >

A server-side HTTP connection that also accepts WebSocket upgrades.

Template Parameters
Derivedderived class for std::enable_shared_from_this.

Constructor & Destructor Documentation

◆ HttpWebSocketServerConnection() [1/2]

template<typename Derived >
wpi::HttpWebSocketServerConnection< Derived >::HttpWebSocketServerConnection ( std::shared_ptr< uv::Stream stream,
std::span< const std::string_view protocols 
)

Constructor.

Parameters
streamnetwork stream
protocolsAcceptable subprotocols

◆ HttpWebSocketServerConnection() [2/2]

template<typename Derived >
wpi::HttpWebSocketServerConnection< Derived >::HttpWebSocketServerConnection ( std::shared_ptr< uv::Stream stream,
std::initializer_list< std::string_view protocols 
)
inline

Constructor.

Parameters
streamnetwork stream
protocolsAcceptable subprotocols

Member Function Documentation

◆ IsValidWsUpgrade()

template<typename Derived >
virtual bool wpi::HttpWebSocketServerConnection< Derived >::IsValidWsUpgrade ( std::string_view  protocol)
inlineprotectedvirtual

Check that an incoming WebSocket upgrade is okay.

This is called prior to accepting the upgrade (so prior to ProcessWsUpgrade()).

The implementation should check other headers and return true if the WebSocket connection should be accepted.

Parameters
protocolnegotiated subprotocol

◆ ProcessWsUpgrade()

template<typename Derived >
virtual void wpi::HttpWebSocketServerConnection< Derived >::ProcessWsUpgrade ( )
protectedpure virtual

Process an incoming WebSocket upgrade.

This is called after the header reader has been disconnected and the websocket has been accepted.

The implementation should set up appropriate callbacks on the websocket object to continue communication.

Note
When a WebSocket upgrade occurs, the stream user data is replaced with the websocket, and the websocket user data points to "this". Replace the websocket user data with caution!

Member Data Documentation

◆ m_websocket

template<typename Derived >
WebSocket* wpi::HttpWebSocketServerConnection< Derived >::m_websocket = nullptr
protected

WebSocket connection; not valid until ProcessWsUpgrade is called.


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