cordova - 带有phonegap/cordova的 meteor

标签 cordova connection meteor

我正在开发一个 meteor 移动应用程序(我使用的是 android)。
我使用 MetoerRider 方法,本质上是 phonegap 应用程序启动,一旦应用程序完成启动,它就会对 meteor 应用程序进行 ajax 调用(“http://myapp.meteor.com”)

当他响应时,我得到了 meteor 应用程序的 DOM。

$.ajax({
      url: __MeteorRiderConfig__.meteorUrl,

      cache: false,

      // TODO: split to method on MeteorRider
      error: function( jqXHR, textStatus, errorThrown ) {

        console.error("MeteorRider failure");

        console.error(jqXHR, textStatus, errorThrown);

      },
      // TODO: split to method on MeteorRider

      success: function( data, textStatus, jqXHR ) {

        console.log("MeteorRider success");

        console.log(textStatus);

        console.log(data);
        // update URLs

        data = data.replace(/(href|src|manifest)\=\"\//gm, '$1="' + meteorUrl + '/');

          console.log(meteorUrl);

        console.log(data);


// get the original file location, not including any params
phonegapLocation = window.location.href.split('.html')[0] + '.html';

// it's stored in a param "page"
currentMeteorPath = window.location.search.replace("?", "")
if(currentMeteorPath.length > 0) {
  meteorUrl += currentMeteorPath.split('page=')[1]
}
console.log("replacing state with "+meteorUrl)
window.history.replaceState({}, "", meteorUrl);  


        // replace the document with the new document/data

        document.open();

        document.write(data);

        document.close();
        // trigger the "loaded" events (it'd be nice to do this AFTER JS has loaded

        $(document).trigger('DOMContentLoaded');

        $(document).trigger('load');

        $(document).trigger('complete');

      }
    });
  }

问题是我的 phonegap 应用程序仅在 wifi 开启时工作,一旦我关闭 wifi,它就会停止工作,如果我在 wifi 关闭时启动我的 phonegap 应用程序,我仍然将 meteor DOM 放入我的 phonegap 应用程序中,但我做不到任何功能(登录/创建组等)。

所以我开始调试我的应用程序(使用 build.phonegap.com 中的 phonegap 调试),
这是我在 wifi 开启时得到的日志:
if(navigator.onLine){
console.log("true")
}
else{
console.log("false")
} 

日志->“真实”
var networkState = navigator.connection.type;

        var states = {};
        states[Connection.UNKNOWN]  = 'Unknown connection';
        states[Connection.ETHERNET] = 'Ethernet connection';
        states[Connection.WIFI]     = 'WiFi connection';
        states[Connection.CELL_2G]  = 'Cell 2G connection';
        states[Connection.CELL_3G]  = 'Cell 3G connection';
        states[Connection.CELL_4G]  = 'Cell 4G connection';
        states[Connection.NONE]     = 'No network connection';

    console.log('Connection type: ' + states[networkState]);
    }

日志 -> 'WiFi 连接'
Meteor.status()

日志 -> 已连接:true
重试次数:0
状态:“已连接”

使用移动网络时:
if(navigator.onLine){
    console.log("true")
    }
    else{
    console.log("false")
    }

日志->“真实”
var networkState = navigator.connection.type;

        var states = {};
        states[Connection.UNKNOWN]  = 'Unknown connection';
        states[Connection.ETHERNET] = 'Ethernet connection';
        states[Connection.WIFI]     = 'WiFi connection';
        states[Connection.CELL_2G]  = 'Cell 2G connection';
        states[Connection.CELL_3G]  = 'Cell 3G connection';
        states[Connection.CELL_4G]  = 'Cell 4G connection';
        states[Connection.NONE]     = 'No network connection';

    console.log('Connection type: ' + states[networkState]);
    }

日志 -> 'Cell 3G 连接'
Meteor.status()

日志 -> 已连接:false
重试次数:1
状态:“正在连接”

试图做 Meteor.reconnect()没有帮助,它不起作用。
当做Meteor.disconnect()当 wifi 打开时,应用程序断开连接,但随后尝试执行 Meteor.reconnect()它也不起作用。

切换时(当应用程序正在运行时)要么打开 wifi 要么关闭 wifi。我失去了所有连接,甚至无法调试。

编辑:添加离线事件
phonegapapp = {
    // are we on a phonegap app?
    phonegap: true,
    // are we testing PhoneGap or not?
    test: false,
    // Application Constructor
    initialize: function () {
        this.bindEvents();
    },
    // Bind Event Listeners
    //
    // Bind any events that are required on startup. Common events are:
    // 'load', 'deviceready', 'offline', and 'online'.
    bindEvents: function () {
        document.addEventListener('deviceready', this.onDeviceReady, false);

    },
    // deviceready Event Handler
    //
    // The scope of 'this' is the event. In order to call the 'receivedEvent'
    // function, we must explicity call 'phonegapapp.receivedEvent(...);'
    onDeviceReady: function () {
        document.addEventListener("offline", this.onOffline, false);
        phonegapapp.receivedEvent('deviceready');
        phonegapapp.receivedEvent('offline');
        if (this.test) {
            $('phonegapapp-test').show();
        } else {
            phonegapapp.meteorRider();
        }
    },
    // Update DOM on a Received Event
    receivedEvent: function (id) {
        console.log('Received Event: ' + id);
    },
    onOffline: function () {
        device.exitApp();
    },

    // Setup MeteorRider
    meteorRider: function () {
        MeteorRider.init();
    }
};

更新:
在两部 android 手机(v 4.1.2 和 v 4.3.3)上,在另一个 meteor 应用程序(没有铁路由器,test.meteor.com)上测试。两部手机都已登录 - Meteor.status() 在使用移动网络时“已连接”。但是当打开/关闭wifi时,两者都失去了任何网络连接。

所以我把它缩小到两个可能的问题:
1.我的 meteor 应用程序出错了。
2.铁路由器

再次检查另一个 meteor 应用程序(使用铁路由器的应用程序)
meteor.status() 记录“已连接”。
我相信失败的原因是在我的应用程序中,虽然我在我的 meteor 应用程序中没有看到任何错误。

新更新:
好的,所以这变得非常奇怪,我已经启动了一个新的 meteor 应用程序,只是在做“ meteor 创建测试”
添加了铁路由器和一些更多的包,如下划线 jquery 帐户等。
然后我已将它部署到 meteor - meteor 部署 boaz.meteor,com
提示输入密码,打开我的 phonegap 应用程序以查看其工作是否已记录 Meteor.status();并得到“假”和“连接”

新更新:
尝试在不进行任何更改或不添加任何包或删除任何包的情况下创建应用程序。
也没有工作,Meteor.status();记录“假”和“连接”

最佳答案

请注册线下事件。如果设备离线,将触发该事件的回调。在回调函数中,关闭(终止/停止/退出)应用程序。

触发离线事件的代码:

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

function onOffline() {
    // Handle the offline event
}

退出 iphone 应用程序:navigator.device.exitApp();
退出安卓应用:device.exitApp();
这样,当连接断开时应用程序将关闭,并且可以再次打开。请告诉我

关于cordova - 带有phonegap/cordova的 meteor ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20441396/

相关文章:

cordova - 有没有办法像使用 href ="tel:"方法一样在 Phonegap 上打开默认电子邮件客户端?

java - 发送 C# 和 Java 的套接字数据 - 总体和具体

javascript - Meteor.WrapAsync 不返回值

Java 数据库立即断开连接

html - 如何更改登录表单的形状?帐户输入和 meteor

javascript - meteor :如何防止客户端访问方法

android - 在 Cordova 插件中使用 SurfaceView

android - 如何让 Meteor Cordova 应用程序允许访问域

cordova - 如何安装先前版本的 PhoneGap/Cordova

java - 关闭java中的数据库连接