Adding isConnected to the JavaScript bridge

* More details https://github.com/webui-dev/webui/issues/392
This commit is contained in:
Hassan DRAGA 2024-06-07 18:31:55 -04:00
parent ced5d9795e
commit 9039c31e68
3 changed files with 803 additions and 759 deletions

File diff suppressed because it is too large Load Diff

View File

@ -176,8 +176,10 @@ class WebuiBridge {
#close(reason = 0, value = '') {
this.#closeReason = reason;
this.#closeValue = value;
if (this.#wsStatus) this.#ws.close();
this.#wsStatus = false;
if (this.#wsStatus) {
this.#wsStatus = false;
this.#ws.close();
}
if (reason === this.#CMD_NAVIGATION) {
if (this.#log) {
console.log(`WebUI -> Close -> Navigation to [${value}]`);
@ -415,7 +417,10 @@ class WebuiBridge {
if (!this.#log) globalThis.close();
else {
console.log(`WebUI -> CMD -> Close`);
if (this.#wsStatus) this.#ws.close();
if (this.#wsStatus) {
this.#wsStatus = false;
this.#ws.close();
}
}
break;
}
@ -450,7 +455,7 @@ class WebuiBridge {
// let's send a void message to keep WS open
this.#sendData(new TextEncoder().encode('ping'));
} else {
// There is an active communicationthis.#wsStatus
// There is an active communication
this.#Ping = true;
}
await new Promise((resolve) => setTimeout(resolve, 20000));
@ -732,6 +737,15 @@ class WebuiBridge {
setEventCallback(callback: (e: number) => void): void {
this.#eventsCallback = callback;
}
/**
* Check if UI is connected to the back-end. The connection
* is done by including `webui.js` virtual file in the HTML.
*
* @return - Boolean `true` if connected
*/
isConnected(): boolean {
return (this.#wsStatus);
}
}
// Export
type webui = WebuiBridge;

View File

@ -7468,14 +7468,12 @@ static WEBUI_THREAD_SERVER_START {
// At this moment the browser is already started and HTML
// is already handled, so, let's wait more time to give
// the WebSocket an extra three seconds to connect.
// the WebSocket an extra seconds to connect.
do {
#ifdef WEBUI_LOG
printf(
"[Core]\t\t_webui_server_thread([%zu]) -> Waiting for "
"first "
"connection\n",
"[Core]\t\t_webui_server_thread([%zu]) -> Waiting for first connection\n",
win->window_number
);
#endif
@ -7487,7 +7485,7 @@ static WEBUI_THREAD_SERVER_START {
for (;;) {
// Stop if window is connected
_webui_sleep(1);
_webui_sleep(10);
if (_webui_mutex_is_connected(win, WEBUI_MUTEX_NONE))
break;
@ -7555,12 +7553,12 @@ static WEBUI_THREAD_SERVER_START {
for (;;) {
// Stop if window is re-connected
_webui_sleep(1);
_webui_sleep(10);
if (_webui_mutex_is_connected(win, WEBUI_MUTEX_NONE))
break;
// Stop if timer is finished
if (_webui_timer_is_end( & timer_3, 1000))
if (_webui_timer_is_end( & timer_3, 3000))
break;
}
} while(win->file_handled && !_webui_mutex_is_connected(win, WEBUI_MUTEX_NONE));