Class Server#

Class Documentation#

class Server#

Multi-client http/websocket server.

See WebUI for more discussion on WebUI.

Public Functions

Server(unsigned short port = 29523)#

Server constructor.

Parameters:

port – - the port to bind to.

~Server()#
template<class T, class R = typename std::invoke_result<T>::type>
inline std::future<R> callAsync(const T &callable)#

Execute the callable on the io thread.

Parameters:

callable – - The callback function

Returns:

A future that resolves to the callback’s return value

template<class T, class R = typename std::invoke_result<T>::type>
inline R callSync(const T &callable)#

Execute the callable on the io thread, blocking until done.

Parameters:

callable – - The callback function

Returns:

The callback’s return value

void sync()#

Wait for currently queued tasks to complete.

std::string guessUrl() const#

Generate a URL to connect to the sever.

Note that this URL is a best guess from the point of view of the server. It may not reflect the publicly accessible URL for the server.

Returns:

The URL guess

unsigned short port() const#

Get the port the server is bound to.

Returns:

The port number.

bool onThread()#

Check whether the current thread is the io thread.

Returns:

Whether the current thread is the io thread

void broadcast(const uint8_t *data, size_t len)#

Send the data buffer to all clients.

This sends the data asynchronously and should only be called from the io thread, eg via callAsync

Parameters:
  • data – - The data to send

  • len – - Number of bytes to send

void sendOne(const uint8_t *data, size_t len)#

Send the data buffer to one client.

This sends the data asynchronously and should only be called from the io thread, eg via callAsync

Parameters:
  • data – - The data to send

  • len – - Number of bytes to send

void onConnect(Connection::handler_t on_connect)#

Send a handler to be called on new connections.

Parameters:

on_connect – - The callback.

void onMessage(Connection::msg_handler_t on_message)#

Send a handler to be called on incoming messages.

Parameters:

on_message – - The callback.

void onClose(Connection::handler_t on_close)#

Send a handler to be called on when a connection closes.

Parameters:

on_close – - The callback.

void waitForClients(int clients = 1, float timeout = 0)#

Wait for a given number of clients to connect.

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

  • timeout – - Timeout in seconds, if positive

void launchLocalClient(const LocalClientOptions &options = LocalClientOptions{})#

Spawn a managed electron client as a subprocess.

Parameters:

options – - Parameters struct for the local client.

void closeLocalClients()#

Close all managed clients.

void printConnectionInfo()#

Print info to stdout about how to connect.

Public Static Functions

static bool canFindLocalClientExecutable()#

Check if the client executable can be found….

Returns:

Whether the client executable can be found