Skip to content

Commit de95086

Browse files
authored
Merge pull request #448 from sensiblearts/main
exposing _webui_get_free_port
2 parents ff97452 + 51d82c8 commit de95086

File tree

3 files changed

+25
-1
lines changed

3 files changed

+25
-1
lines changed

include/webui.h

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -707,6 +707,15 @@ WEBUI_EXPORT size_t webui_get_child_process_id(size_t window);
707707
*/
708708
WEBUI_EXPORT bool webui_set_port(size_t window, size_t port);
709709

710+
/**
711+
* @brief Get an available network port to be used by your app or by WebUI.
712+
*
713+
* @return Returns a free port
714+
*
715+
* @example int port = webui_get_free_port(); webui_set_port(myWindow, port);
716+
*/
717+
WEBUI_EXPORT size_t webui_get_free_port(void);
718+
710719
/**
711720
* @brief Control the WebUI behaviour. It's recommended to be called at the beginning.
712721
*

include/webui.hpp

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -180,6 +180,9 @@ class window {
180180
// link of `webui.js` in case you are trying to use WebUI with an external web-server like NGNIX
181181
void set_port(size_t port) const { webui_set_port(webui_window, port); }
182182

183+
// Get an available network port to be used by your app or by WebUI.
184+
size_t get_free_port() { webui_get_free_port(); }
185+
183186
// Set window position
184187
void set_position(unsigned int x, unsigned int y) const { webui_set_position(webui_window, x, y); }
185188

src/webui.c

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -473,7 +473,7 @@ static void * _webui_run_browser_task(void * _arg);
473473
static void _webui_init(void);
474474
static bool _webui_show(_webui_window_t* win, struct mg_connection* client, const char* content, size_t browser);
475475
static bool _webui_get_cb_index(_webui_window_t* win, const char* element, size_t* id);
476-
static size_t _webui_get_free_port(void);
476+
static size_t _webui_get_free_port(void);
477477
static void _webui_free_port(size_t port);
478478
static char* _webui_get_current_path(void);
479479
static void _webui_send_client_ws(_webui_window_t* win, struct mg_connection* client,
@@ -2071,6 +2071,18 @@ size_t webui_get_parent_process_id(size_t window) {
20712071
return win->process_id;
20722072
}
20732073

2074+
size_t webui_get_free_port(void) {
2075+
2076+
#ifdef WEBUI_LOG
2077+
printf("[User] webui_get_free_port()\n");
2078+
#endif
2079+
2080+
// Initialization
2081+
_webui_init();
2082+
2083+
return _webui_get_free_port();
2084+
}
2085+
20742086
#ifdef WEBUI_TLS
20752087
static bool _webui_check_certificate(const char* certificate_pem, const char* private_key_pem) {
20762088

0 commit comments

Comments
 (0)