Class HttpWsServer#

Class Documentation#

class HttpWsServer#

Generic http/websocket server.

See WebUI for more discussion on WebUI.

Public Functions

HttpWsServer(unsigned short port)#

HttpWsServer constructor.

Parameters:

port – - the port to bind to.

virtual ~HttpWsServer()#

HttpWsServer destructor.

unsigned short getPort() const#

Get the port the server is bound to.

Returns:

The port number.

std::string getUrl() const#

Get the url to access the server.

Returns:

The url.

void serveFile(std::string_view url, const std::filesystem::path &path, const HeaderList &headers = {})#

Serve a give file for GET requests.

If headers is empty, Content-Type will automatically be added based on MIME type associated with the filename extension

Parameters:
  • url – - The url to serve at

  • path – - Path to the file to serve

  • headers – - Response headers

void serveData(std::string_view url, std::string_view content, const HeaderList &headers = {})#

Serve the given string for GET requests.

If headers is empty, Content-Type will automatically be added with the type, application/octet-stream

Parameters:
  • url – - The url to serve at

  • content – - The data to serve

  • headers – - Response headers

std::string serveCasFile(const std::filesystem::path &path, std::string_view url_prefix = "/objects/", const HeaderList &headers = {})#

Idempotently serve a give file at a content-addressed URL.

If headers is empty, automatically determines MIME_TYPE based on file extension and uses the headers:

  • Content-Type: $MIME_TYPE

  • Cache-Control: public, max-age=31536000, immutable

Parameters:
  • path – - Path to the file to serve

  • url_prefix – - A url to prepend to the CAS url

  • headers – - Response headers

Returns:

The content-addressed URL

std::string serveCasData(std::string_view content, std::string_view url_prefix = "/objects/", const HeaderList &headers = {})#

Idempotently serve the given string at a content-addressed URL.

If headers is empty, automatically uses the headers:

  • Content-Type: application/octet-stream

  • Cache-Control: public, max-age=31536000, immutable

Parameters:
  • content – - The data to serve

  • url_prefix – - A url to prepend to the CAS url

  • headers – - Response headers

Returns:

The content-addressed URL

void broadcastMessage(std::string_view msg)#

Send a message to all connected websocket clients.

Parameters:

msg – - The message to send

void sendMessage(std::string_view msg, int client_id)#

Send a message to a given websocket client.

Parameters:
  • msg – - The message to send

  • client_id – - The id for the client to send to

void setOnConnect(std::function<void(int)> callback = {})#

Set a function called on websocket client connection.

Parameters:

callback – - The callback function

void setOnMessage(std::function<void(std::string_view, int)> callback = {})#

Set a function called on receiving a websocket message.

Parameters:

callback – - The callback function

void setOnDisconnect(std::function<void(int)> callback = {})#

Set a function called on websocket client disconnect.

Parameters:

callback – - The callback function

bool waitForClients(int clients = 1, float timeout_seconds = 0)#

Wait for a given number of clients to connect.

Parameters:
  • clients – - Number of clients to wait for

  • timeout_seconds – - Timeout, if positive

Returns:

True if the requested client count was reached

void defer(std::function<void()> callback)#

Submit a function to be called in the event loop.

Parameters:

callback – - The callback function

void sync()#

Block for deferred calls.

This places a deferred callback on the event loop and waits for the callback to be called, ensuring any prior callbacks have also been called. This does not wait for the event loop to reach quiescence.

void printConnectionInfo()#

Print info to stdout about how to connect.

void close()#

Shut down the server.