且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

如何防止Meteor / Cordova应用程序连接到10.0.2.2? (为什么应用程序连接到那里?)

更新时间:2022-11-01 08:18:29

所以即使你设置了 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.