@@ -77,6 +77,7 @@ class WebuiBridge {
7777 #Ping: Boolean = true ;
7878 // Events
7979 #eventsCallback: ( ( event : number ) => void ) | null = null ;
80+ #lastEvent: number = - 1 ;
8081 event = {
8182 // TODO: Make `event` static and solve the ESBUILD `_WebuiBridge` issue.
8283 CONNECTED : 0 ,
@@ -127,7 +128,7 @@ class WebuiBridge {
127128 }
128129 // WebSocket
129130 if ( ! ( 'WebSocket' in window ) ) {
130- alert ( 'Sorry. WebSocket is not supported by your web browser. ') ;
131+ this . #showWarning ( ' WebSocket is not supported by your web browser') ;
131132 if ( ! this . #log) globalThis . close ( ) ;
132133 }
133134 // Connect to the backend application
@@ -210,7 +211,7 @@ class WebuiBridge {
210211 } ;
211212 setTimeout ( ( ) => {
212213 if ( ! this . #wsWasConnected) {
213- alert ( 'Sorry. WebUI failed to connect to the backend application. Please try again. ') ;
214+ this . #showWarning ( 'Failed to connect to the backend application') ;
214215 }
215216 } , 1500 ) ;
216217 }
@@ -222,31 +223,38 @@ class WebuiBridge {
222223 this . #ws. close ( ) ;
223224 }
224225 }
225- #freezeUi ( ) {
226+ #showWarning ( msg : string ) {
226227 setTimeout ( ( ) => {
227228 if ( ! this . #wsIsConnected( ) ) {
228229 if ( document . getElementById ( 'webui-error-connection-lost' ) ) return ;
229230 const div = document . createElement ( 'div' ) ;
230231 div . id = 'webui-error-connection-lost' ;
231232 Object . assign ( div . style , {
232- position : 'relative ' ,
233+ position : 'fixed ' ,
233234 top : '0' ,
234235 left : '0' ,
235236 width : '100%' ,
236- backgroundColor : '#ff4d4d ' ,
237+ backgroundColor : 'rgb(191 125 30 / 84%) ' ,
237238 color : '#fff' ,
238239 textAlign : 'center' ,
239- padding : '2px 0' ,
240+ padding : '4px 0' ,
240241 fontFamily : 'Arial, sans-serif' ,
241- fontSize : '14px ' ,
242- zIndex : '1000 ' ,
242+ fontSize : '16px ' ,
243+ zIndex : '9000 ' ,
243244 lineHeight : '1'
244245 } ) ;
245- div . innerText = 'WebUI Error: Connection with the backend is lost. ' ;
246+ div . innerHTML = '<strong> Error:</strong> ' + msg + ' - <em>WebUI 2.5.0-beta.4</em> ';
246247 document . body . insertBefore ( div , document . body . firstChild ) ;
247248 }
248249 } , 1000 ) ;
249250 }
251+ #freezeUi( ) {
252+ if ( this . #eventsCallback == null ) {
253+ // Show default WebUI warning as we
254+ // don't have a user event callback.
255+ this . #showWarning( 'Connection with the backend is lost' ) ;
256+ }
257+ }
250258 #unfreezeUI( ) {
251259 const div = document . getElementById ( 'webui-error-connection-lost' ) ;
252260 if ( div ) {
@@ -657,10 +665,16 @@ class WebuiBridge {
657665 }
658666 }
659667 // Event Callback
668+ this . #userEventCallback( this . event . DISCONNECTED ) ;
669+ } ;
670+ #userEventCallback = ( e : number ) => {
660671 if ( this . #eventsCallback) {
661- this . #eventsCallback( this . event . DISCONNECTED ) ;
672+ if ( e != this . #lastEvent) {
673+ this . #lastEvent = e ;
674+ this . #eventsCallback( e ) ;
675+ }
662676 }
663- } ;
677+ }
664678 #wsOnMessage = async ( event : MessageEvent ) => {
665679 const buffer8 = new Uint8Array ( event . data ) ;
666680 if ( buffer8 . length < this . #PROTOCOL_SIZE) return ;
@@ -859,9 +873,7 @@ class WebuiBridge {
859873 // Generate objects
860874 this . #updateBindsList( ) ;
861875 // User event callback
862- if ( this . #eventsCallback) {
863- this . #eventsCallback( this . event . CONNECTED ) ;
864- }
876+ this . #userEventCallback( this . event . CONNECTED ) ;
865877 }
866878 else {
867879 if ( this . #log) console . log ( `WebUI -> CMD -> Token [${ tokenHex } ] Not Accepted. Reload page...` ) ;
0 commit comments