Class HttpWsServer#
Defined in File HttpWsServer.h
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, std::string_view mime_type = "")#
Serve a give file for GET requests.
- Parameters:
url – - The url to serve at
path – - Path to the file to serve
mime_type – - The MIME type of data; inferred if omitted
-
void serveData(std::string_view url, std::string_view content, std::string_view mime_type = "application/octet-stream")#
Serve the given string for GET requests.
- Parameters:
url – - The url to serve at
content – - The data to serve
mime_type – - The MIME type of the data
-
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.
-
HttpWsServer(unsigned short port)#