Skip to content

Commit 9ed9279

Browse files
committed
Added a function webui_get_port() to retrieve the server port for the session
1 parent de95086 commit 9ed9279

File tree

3 files changed

+34
-0
lines changed

3 files changed

+34
-0
lines changed

include/webui.h

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -693,6 +693,20 @@ WEBUI_EXPORT size_t webui_get_parent_process_id(size_t window);
693693
*/
694694
WEBUI_EXPORT size_t webui_get_child_process_id(size_t window);
695695

696+
/**
697+
* @brief Get the web-server network port to be used by WebUI.
698+
* This can be useful to determine the HTTP link of `webui.js`
699+
*
700+
* @param window The window number
701+
* @param port The web-server network port WebUI should use
702+
*
703+
* @return Returns 0 if no port is used, or the port used by
704+
* web-server network port WebUI should use
705+
*
706+
* @example size_t port = webui_get_port(myWindow);
707+
*/
708+
WEBUI_EXPORT size_t webui_get_port(size_t window);
709+
696710
/**
697711
* @brief Set a custom web-server/websocket network port to be used by WebUI.
698712
* This can be useful to determine the HTTP link of `webui.js` in case

include/webui.hpp

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -176,6 +176,9 @@ class window {
176176
// Set window size
177177
void set_size(unsigned int width, unsigned int height) const { webui_set_size(webui_window, width, height); }
178178

179+
// Get the web-server network port to be used by WebUI. Returns 0 if no port is used.
180+
size_t get_port() const { return webui_get_port(webui_window); }
181+
179182
// Set a custom web-server network port to be used by WebUI. This can be useful to determine the HTTP
180183
// link of `webui.js` in case you are trying to use WebUI with an external web-server like NGNIX
181184
void set_port(size_t port) const { webui_set_port(webui_window, port); }

src/webui.c

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2291,6 +2291,23 @@ void webui_set_event_blocking(size_t window, bool status) {
22912291
win->ws_block = status;
22922292
}
22932293

2294+
size_t webui_get_port(size_t window) {
2295+
2296+
#ifdef WEBUI_LOG
2297+
printf("[User] webui_get_port([%zu])...\n", window);
2298+
#endif
2299+
2300+
// Initialization
2301+
_webui_init();
2302+
2303+
// Dereference
2304+
if (_webui_mutex_is_exit_now(WEBUI_MUTEX_NONE) || _webui.wins[window] == NULL)
2305+
return 0;
2306+
_webui_window_t* win = _webui.wins[window];
2307+
2308+
return win->server_port;
2309+
}
2310+
22942311
bool webui_set_port(size_t window, size_t port) {
22952312

22962313
#ifdef WEBUI_LOG

0 commit comments

Comments
 (0)