Added a function webui_get_port() to retrieve the server port for the session

This commit is contained in:
Nathan R 2024-07-09 14:39:38 -07:00
parent de9508653a
commit 9ed9279a98
3 changed files with 34 additions and 0 deletions

View File

@ -693,6 +693,20 @@ WEBUI_EXPORT size_t webui_get_parent_process_id(size_t window);
*/
WEBUI_EXPORT size_t webui_get_child_process_id(size_t window);
/**
* @brief Get the web-server network port to be used by WebUI.
* This can be useful to determine the HTTP link of `webui.js`
*
* @param window The window number
* @param port The web-server network port WebUI should use
*
* @return Returns 0 if no port is used, or the port used by
* web-server network port WebUI should use
*
* @example size_t port = webui_get_port(myWindow);
*/
WEBUI_EXPORT size_t webui_get_port(size_t window);
/**
* @brief Set a custom web-server/websocket network port to be used by WebUI.
* This can be useful to determine the HTTP link of `webui.js` in case

View File

@ -176,6 +176,9 @@ class window {
// Set window size
void set_size(unsigned int width, unsigned int height) const { webui_set_size(webui_window, width, height); }
// Get the web-server network port to be used by WebUI. Returns 0 if no port is used.
size_t get_port() const { return webui_get_port(webui_window); }
// Set a custom web-server network port to be used by WebUI. This can be useful to determine the HTTP
// link of `webui.js` in case you are trying to use WebUI with an external web-server like NGNIX
void set_port(size_t port) const { webui_set_port(webui_window, port); }

View File

@ -2291,6 +2291,23 @@ void webui_set_event_blocking(size_t window, bool status) {
win->ws_block = status;
}
size_t webui_get_port(size_t window) {
#ifdef WEBUI_LOG
printf("[User] webui_get_port([%zu])...\n", window);
#endif
// Initialization
_webui_init();
// Dereference
if (_webui_mutex_is_exit_now(WEBUI_MUTEX_NONE) || _webui.wins[window] == NULL)
return 0;
_webui_window_t* win = _webui.wins[window];
return win->server_port;
}
bool webui_set_port(size_t window, size_t port) {
#ifdef WEBUI_LOG