IBM Worklight - Connecting/Re-Connecting: WL.Clien

2019-02-11 02:05发布

问题:

In our project we are trying to figure our what the best process to connect to the server will be - especially when taking care of offline/online scenarios etc.

Right now, for us, it seems that all three options to connect to the WL server are similar. Whatever option we use, we can call our Adapter Procedures perfectly and we receive Notification Messages that are set in the console. We are not sure about Direct Update - this is not working properly yet.

Are there any important differences between these three ways of connecting to the WL server, or is basically the same connection procedure being executed in all three cases?

How about WL.Client.init() before connecting - could we call that again (in addition to the standard window load EventListener) in our code before we connect using a WL.Client.connect manually - or is init() supposed to be called only once?

Tied to that is also offline and re-connecting. As far as I have read in the tutorials, the WL Client framework is managing the connection state. Does that mean that when the WL client is connected to the server through any of these three ways and loses it's WLAN/3G/4G connection (or it's access to the WL server due to internet connection blocker or so) it re-connects automatically (regularly tries to re-connect until successful) when a connection to the WL server is available again?

EDIT

I was thinking about Events or Threads that provide more low-level information (not WORKLIGHT_IS_CONNECTED) - basically events that would be triggered when the device loses/gets WIFI/3G/4G connection and/or internet connection. Or is there only polling using WL.Device.getNetworkInfo() available?

Would the use of Cordova Event like:

document.addEventListener("offline", yourCallbackFunction, false);

provide a functionality close to that?

回答1:

Though those three ways do have some similarities the differences between them are very important.

WL.Client.init() initializes client side WL framework.

WL.Client.connect() triggers connection request to WL server.

initOptions.connectOnStartup defines whether WL.Client.connect() will be invoked automatically during WL.Client.init().

In general - most of the functionality (e.g. adapters, remote disable) will function even if you call WL.Client.invokeProcedure() without calling WL.Client.connect(). But there are several things that will not function:

  1. You will not be able to fully utilize push notifications without calling WL.Client.connect()
  2. Direct update is triggered during WL.Client.connect()
  3. WL.Client.connect() will get security related info from server, e.g. names of realms, whether user authenticated in those realms etc. Therefore all APIs like WL.Client.getUserInfo, .isAuthenticated(), .getUserName() etc will not function.

It is strongly recommended to start your session with WL.Client.connect() (or initOptions.connectOnStartup=true).