Page 1 of 1

How to check if offline or online (quickly)

Posted: Fri May 28, 2021 10:53 am
by edz.rama
I'm building an app with offline capability and I need a way to check quickly if I'm offline or not .
I'm currently using #SYS_WEB.online but it seem unreliable so I do a call to the server module and if it failed that means I'm offline.
It is working but it takes about 20 seconds so I want a better way of handling it.

Here's the code that I'm working on:

Evtroutine Handling(#SaveIcon.Click)
If (#COM_OWNER.Validate( Update #xContacts ))
* this don't appear to be working
If (#SYS_WEB.online *EQ True)
#COM_OWNER.Save

Else
#COM_OWNER.SaveOffline
Endif
Endif

Endroutine



Mthroutine Name(Save) Access(*PRIVATE)

Define_Com Class(#EX_DataServer.Save) Name(#Save)

#SYS_MSGQ.ClearAll

#ErrorText.Visible := False

#Save.ExecuteAsync( #Fields #STD_BLOB #STD_CODE )

Evtroutine Handling(#Save.Completed)

If (#STD_CODE = "OK")

#COM_OWNER.Close
Signal Event(ItemSaved) Action(#DF_EFLAG) Id(#xContactIdentification)

Else

#ErrorText.Caption := ""

For Each(#Message) In(#SYS_MSGQ.Messages)

#ErrorText.Caption += #Message.Text + (10).AsUnicodeString

Endfor

#ErrorText.Visible := True

#Details.VerticalScrollPos := 0

* #COM_OWNER.Close
Endif

Endroutine

Evtroutine Handling(#Save.Failed) Handled(#handled)
#COM_OWNER.SaveOffline
#handled := true
#COM_OWNER.Close
Endroutine
Endroutine


Thanks,
Eduardo

Re: How to check if offline or online (quickly)

Posted: Thu Jun 03, 2021 3:59 pm
by dominique
Hi Eduardo

You can use the navigator information
https://developer.mozilla.org/en-US/doc ... ine/onLine

1. create a widget
2. add methode
methode.jpg
methode.jpg (32.59 KiB) Viewed 20697 times
3. add eventlistener (optional)

Code: Select all

function( PROTOTYPE, WIDGET )
{

window.addEventListener('offline', function(e) { console.log('offline'); });
window.addEventListener('online', function(e) { console.log('online'); });

...
return WIDGET.Completed;

4. finalize Methode

Code: Select all

  //
  // isOnline - Check if Device has connection
  //
  // Return Type: Boolean
  //
  PROTOTYPE.isOnline = function()
  {
    return window.navigator.onLine;
  }
5. Integrate Widget

Code: Select all

Define_Com Class(#wMobileWebAppInfo) Name(#wMobileWebAppInfo) Scope(*APPLICATION) 

Mthroutine Name(isOnline)
Define_Map For(*RESULT) Class(#PRIM_BOLN) Name(#O_Result Result)

#O_Result := #wMobileWebAppInfo.isOnline

Endroutine

6. Test widget in browser
Test.jpg
Test.jpg (165.07 KiB) Viewed 20697 times
Hope that helps

Re: How to check if offline or online (quickly)

Posted: Fri Jun 04, 2021 2:46 pm
by edz.rama
I was hoping there's a quick way to check if you can connect to the server or not but I guess the request timeout differs from browser to browser.
But this will be helpful to 'mimic' offline/online when testing. Thanks

Re: How to check if offline or online (quickly)

Posted: Fri Jun 04, 2021 9:31 pm
by dannyoorburg
Isn't that exactly what #sys_web.online does?

https://docs.lansa.com/15/en/lansa016/c ... online.htm

Re: How to check if offline or online (quickly)

Posted: Mon Jun 07, 2021 1:46 pm
by dominique
Yes, it's probably the same as sys_web.online. Using the navigator.online is the most common way to check if a connection is etablished or not. The only difference is the eventlistener.