How to check if offline or online (quickly)

This Q&A forum allows users to post and respond to "How Do I Do ....." questions. Please do not use to report (suspected) errors - you must use your regional help desk for this. The information contained in this forum has not been validated by LANSA and, as such, LANSA cannot guarantee the accuracy of the information.
Post Reply
edz.rama
Posts: 2
Joined: Fri Mar 16, 2018 11:31 am

How to check if offline or online (quickly)

Post by edz.rama » Fri May 28, 2021 10:53 am

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

dominique
Posts: 41
Joined: Mon May 29, 2017 4:16 pm
Location: St. Gallen, Switzerland

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

Post by dominique » Thu Jun 03, 2021 3:59 pm

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 413 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 413 times
Hope that helps

edz.rama
Posts: 2
Joined: Fri Mar 16, 2018 11:31 am

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

Post by edz.rama » Fri Jun 04, 2021 2:46 pm

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

dannyoorburg
Posts: 177
Joined: Mon Jan 04, 2016 9:50 am
Location: Australia

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

Post by dannyoorburg » Fri Jun 04, 2021 9:31 pm

Isn't that exactly what #sys_web.online does?

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

dominique
Posts: 41
Joined: Mon May 29, 2017 4:16 pm
Location: St. Gallen, Switzerland

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

Post by dominique » Mon Jun 07, 2021 1:46 pm

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.

Post Reply