【问题标题】:Worklight Direct Update and run offlineWorklight Direct 更新和脱机运行
【发布时间】:2025-11-26 02:20:12
【问题描述】:

我想实现这样的功能。 那是: 1) 成功连接worklight服务器的情况下,可以直接更新。 2) 在连接worklight服务器失败的情况下,应用可以离线运行。

下面是我在“initOptions.js”中的配置。

// # Should application automatically attempt to connect to Worklight Server on                           application start up
// # The default value is true, we are overriding it to false here.
connectOnStartup : true,

// # The callback function to invoke in case application fails to connect to Worklight Server
onConnectionFailure: function (){
    alert("onConnectionFailure");
    doDojoReady();
},

// # Worklight server connection timeout
timeout: 10 * 1000,

// # How often heartbeat request will be sent to Worklight Server
heartBeatIntervalInSecs: 20 * 60,

// # Should application produce logs
// # Default value is true
//enableLogger: false,

// # The options of busy indicator used during application start up
busyOptions: {text: "Loading..."

但它不起作用。 有什么想法吗?

【问题讨论】:

  • 我正在使用 Worklight 版本:5.0.5。当网络可用时,直接更新工作正常;但是离线时,我的应用程序无法运行。

标签: ibm-mobilefirst


【解决方案1】:

仅当与服务器的连接可用时才会发生直接更新。从您提出问题的方式来看,您的问题是当应用程序无法连接到服务器时,它不能“离线”工作。因此,您的问题与直接更新无关(如果有,请适当地重新表述您的问题)。

你应该做的是read the training material for working offline in Worklight

您没有指定什么“不起作用”。您是否收到您在 onConnectionFailure 中放置的警报?你的 doDojoReady 函数是什么样子的?

【讨论】:

  • 感谢您的链接,伊丹。我调用了错误的方法。我应该在 onConnectionFailure 中调用 wlCommonInit 函数。 onConnectionFailure: function (){ wlCommonInit (); },
【解决方案2】:

我也在 Worklight 中使用 Dojo。

我的做法是将工作灯配置为在启动时连接

 var wlInitOptions = {  
     connectOnStartup : false

然后在我的 wl init 中初始化我的 dojo 应用程序,

 function wlCommonInit(){
  loadDojoLayers(); 
 };

需要我使用的任何层,然后进行实际的 dojo 解析

 require([ "dojo/parser",
          "myApp/appController",
          "dojo/domReady!"
        ],
       function(parser, appController) {                
           parser.parse().then (function(){
                appController.init();
       });          
});

最后,现在 WL、Dojo 和 myApp 都准备好了,我尝试 WL 连接,从我的 appController.init() 调用此方法

connectWLServer: function() {

      // possibly WL.Client.login(realm) here

       var options = {
             onSuccess: lang.hitch(this, "connectedWLServer"),
             onFailure: lang.hitch(this, "connectWLServerFailed"),
       };

       WL.Client.connect(options);
}

此时会发生任何直接更新活动。请注意,无论连接是否有效,整个应用程序都会继续运行,但显然我们可以在成功和失败的情况下运行适当的代码。根据具体需要什么身份验证,可能需要显式登录调用 - 基于适配器的身份验证不能从 connect() 内部自动发生。

【讨论】: