ark::comms::HttpServer
Defined in header “ark/comms/http_server.hh”.
This is a simple HTTP server. It allows you to register callbacks at URLs, so that you can provide custom information to external clients.
This class can also act as a websocket server, allowing you to register ‘streams’ that clients may subscribe to. This acts as a simple publish/subscribe mechanism.
Methods
-
HttpServer(uint16_t port_number)
Constructor. Initializes the HTTP server on the given port number. Immediately begins accepting connections. -
HttpServer(const NetworkAddress & address)
Constructor. Initializes the HTTP server against the given address. Immediately begins accepting connections. -
HttpServer(const HttpServerConfig & config)
Constructor. Initializes the HTTP server with the given configuration. Immediately begins accepting connections. -
HttpServer()
Disable copies. We don’t want multiple people polling on the same socket at the same time. -
HttpServer & operator=()
-
~HttpServer()
Private destructor. -
NetworkAddress address()
Returns the address that this server is running at. -
void poll(std::chrono::nanoseconds duration)
Executes a polling interval for the server. This will run a single poll, and handle incoming data from all clients. The duration indicates how long you wish to wait for input before returning.Note that this routine will dispatch to callbacks if requests have come in, so could block for an arbitrary amount of time.
-
size_t clients_connected()
Returns the number of clients currently connected. -
void add_static_callback(const std::string & path, const StaticMemoryCallback & callback)
Binds a function to a particular path. If this path is ever accessed, this function will be invoked. -
void add_static_callback(const std::string & path, const StaticFileCallback & callback, const std::filesystem::path & request_base_dir)
Binds a function to a particular path. If this path is ever accessed, this function will be invoked. This differs from the standard static callback in that the request body is written to disk at the given location, rather than store in memory. -
void add_regex_callback(const std::string & path, const StaticMemoryCallback & callback)
Binds a function to a particular path, which is matched by a regex. This function will be invoked each time the regex matches an incoming path. -
void add_streaming_callback(const std::string & path, const StreamingCallbacks & callbacks)
Binds the given callbacks to the specified path. When a websocket request comes in on this path, these callbacks will be invoked. -
HttpServerStatistics statistics()
Returns statistics related to the web server.