【问题标题】:meteor with phonegap/cordova带phonegap/cordova的流星
【发布时间】:2013-12-24 20:05:27
【问题描述】:

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

当他响应时,我得到了流星应用程序的 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()

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

试图做Meteor.reconnect() 没有帮助并且它不起作用。 在打开 wifi 的情况下执行 Meteor.disconnect() 时,应用程序会断开连接,但是当尝试执行 Meteor.reconnect() 时,它也无法正常工作。

在切换(当应用程序运行时)打开/关闭 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)和另一个流星应用程序(没有 Iron-router,test.meteor.com)上测试。两部手机都记录了 - Meteor.status() 在使用移动网络时“已连接”。但是当打开/关闭wifi时,两者都失去了任何网络连接。

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

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

新更新: 好的,所以这真的很奇怪,我已经开始了一个新的流星应用程序,只需执行“流星创建测试” 添加了 Iron-router 和一些更多的包,比如下划线 jquery 帐户等。 然后我将它部署到流星-meteor deploy boaz.meteor,com 提示输入密码,打开我的 phonegap 应用程序,看看它是否工作记录 Meteor.status();并得到“假”和“连接”

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

【问题讨论】:

    标签: cordova connection meteor


    【解决方案1】:

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

    触发离线事件代码:

    document.addEventListener("offline", onOffline, false);
    
    function onOffline() {
        // Handle the offline event
    }
    

    退出 iphone 应用程序:navigator.device.exitApp();

    退出安卓应用:device.exitApp();

    这样,应用程序将在连接断开时关闭,然后可以再次打开。请告诉我

    【讨论】:

    • 不,那个 dosent 似乎也可以工作,有两个主要问题: 1. phonegapp 应用程序 dosent 连接到 Meteor 服务器 - Meteor.status() 在使用时总是返回 false 和“正在连接”移动网络。 2.当打开/关闭 wifi 时,所有连接都会丢失,无论我使用什么(wifi 或移动网络)。对于第一个问题,我很确定它与 Meteor 相关,也许是网络套接字?我也很确定,如果将 config.xml 中的内容 src 设置为我的 Meteor 应用程序,我不会遇到这些问题。 (虽然我还没试过)
    • 离线事件也是基于 Connection 对象的,所以当 Connection.NONE 为真时,会触发离线事件(据我所知,如果我错了,请纠正我),但 Connection 对象永远不会更改为没有。
    猜你喜欢
    • 1970-01-01
    • 2014-08-19
    • 2015-09-01
    • 2013-12-07
    • 2019-04-10
    • 1970-01-01
    • 2015-05-26
    • 1970-01-01
    • 2017-08-12
    相关资源
    最近更新 更多