如何防止Meteor / Cordova应用程序连接到10.0.2.2? (为什么应用程序连接到那里?) [英] How to prevent a Meteor/Cordova App from connecting to 10.0.2.2? (And why does the app connect there?)

查看:144
本文介绍了如何防止Meteor / Cordova应用程序连接到10.0.2.2? (为什么应用程序连接到那里?)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个在本地服务器上运行的Meteor应用程序( http://10.0.2.10:3000)。 ROOT_URL 设置正确,因此 __ meteor_runtime_config __。ROOT_URL 等于此URL。当然,这个应用程序在10.0.2.0/24内的客户端计算机上的浏览器中工作正常。该应用程序也工作正常的移动chrome / firefox在我的Android手机,这也是10.0.2.0/24的一部分。但是,当我尝试运行它作为应用程序在这个手机与流星运行android-device --mobile-server http://10.0.2.10:3000/ 一些奇怪的事情:



当应用程序第一次启动时(或清除所有应用程序数据后的第一次),它的工作原理应该是(来自数据库的内容)几秒钟。然后应用程序重新加载,并且数据库中的任何远程内容不再被加载。我添加了以下函数来查看Meteor尝试连接到的位置:

  Meteor.startup(function(){
console.log(__ meteor_runtime_config __。ROOT_URL);
})

内容加载,这会返回 http://10.0.2.10:3000/ ,就像我预期的那样。第二次未加载远程内容时,会返回 http://10.0.2.2:3000/ 。 / p>

现在的问题是,为什么Meteor / Cordova会这样做,如何阻止这种行为?因为显然我不能用这种方式测试应用程序。我还不确定它是否会在生产中工作,当我有一个FQDN和HTTPS代理,但这是超出了点。



我试图找到10.0.2.2没有在我的局域网运行在那里,我没有指定这个IP在任何地方,并发现在 /cordova-build/www/application/index.html 这似乎是从 boilerplate_web.cordova.html (请参阅此链接 https:/ /searchcode.com/codesearch/view/91819963/ )。但是Meteor提供了用文件夹 cordova-build-override 覆盖这些生成的文件的可能性,因此我删除了整个

  if(/Android/i.test(navigator.userAgent)){
// [...]
}

阻止并添加了一个短 console.log('removed')。这被调用所以我知道重写是成功的,当我grep通过整个构建的.apk文件10.0.2.2不再被发现 - 仍然的行为是一样的。


解决方案

所以即使你设置了 ROOT_URL 正确地,仍然有一些特殊的变量,它的手机版本没有设置,可能包含 localhost 。并且在meteor项目中似乎存在更多的代码片段,除了 localhost 10.0.2.2 当Cordova客户端正在连接时。所以这似乎导致我的应用程序重新连接到10.0.2.2。



我可以找到的移动网址变量是
process.env .MOBILE_ROOT_URL
process.env.MOBILE_DDP_URL 。因此,在 Meteor.startup()函数中,我现在将其设置为服务器端的真实 ROOT_URL 。我的Android(Cordova)应用程序现在仍然是在第一次启动后几秒钟重新连接,但它重新连接到相同(和真实的)服务器URL(因此一切正常)!



我还不知道为什么它的重新连接和那些移动变量和它们的使用似乎没有很好的记录(或我错过了一些东西),但我可以生活的方式的事情现在工作。


I have a Meteor app which runs on a local server for developement (http://10.0.2.10:3000). The ROOT_URL is set correctly so __meteor_runtime_config__.ROOT_URL equals this URL. Of course the app is working perfectly fine in the browser on a client computer within 10.0.2.0/24. The app is also working fine on mobile chrome/firefox on my android cell phone which is also part of 10.0.2.0/24. However when I try to run it as app on this cell phone with meteor run android-device --mobile-server http://10.0.2.10:3000/ something strange happens:

When the app starts for the first time (or the first time after I clear all app data) it works like it should (content from the DB is loaded) for a few short seconds. Then the app reloads and any remote content from the DB isn't loaded anymore. I have added the following function to see where Meteor tries to connect to:

Meteor.startup(function(){
    console.log(__meteor_runtime_config__.ROOT_URL);
})

The first time when remote content is loaded this returns http://10.0.2.10:3000/ like I would expect. The second time when remote content isn't loaded it returns http://10.0.2.2:3000/.

The question now is, why is Meteor/Cordova doing this and how can I stop this behavior? Because obviously I cannot test the app this way. I'm not yet sure if it would work in production when I have a FQDN and HTTPS proxy but that's beyond the point.

I tried to find 10.0.2.2 as nothing in my LAN is running there and I have not specified this IP anywhere and found it in /cordova-build/www/application/index.html which seems to be generated from boilerplate_web.cordova.html (see this link https://searchcode.com/codesearch/view/91819963/). However Meteor offers the possibility to override these generated files with a folder cordova-build-override and so I did removing the whole

if (/Android/i.test(navigator.userAgent)) {
    //[...]
}

block and added a short console.log('removed'). This gets called so I know the override was successful and when I grep through the whole built .apk file 10.0.2.2 isn't found anymore - still the behavior is the same.

Any ideas what's going on and what to do?

解决方案

So even when you set your ROOT_URL correctly there are still special variables for mobile versions of it which do not get set and may contain localhost. And there seem to exist more code snippets in the meteor project which replace localhost with 10.0.2.2 aside from the one I mentioned above, when a Cordova client is connecting. So that seems to cause my app to reconnect to 10.0.2.2.

The mobile URL variables I could find are process.env.MOBILE_ROOT_URL and process.env.MOBILE_DDP_URL. So in a Meteor.startup() function I now set those to my real ROOT_URL on the server side. My Android (Cordova) app now still is reconnecting a few seconds after its first start up but it's reconnecting to the same (and real) server URL (thus everything works fine)!

I still don't know why its reconnecting and those mobile variables and their use don't seem to be very well documented (or I missed something) but I can live with the way things work now.

这篇关于如何防止Meteor / Cordova应用程序连接到10.0.2.2? (为什么应用程序连接到那里?)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆