从本地网络访问 firebase 模拟器

Kin*_*ien 21 firebase firebase-tools google-cloud-functions

我正在使用firebase emulators:startfirebase 命令在本地模拟功能。这在 iOS 模拟器上运行良好,但如果我使用同一网络中的真实 android 设备,则无法访问这些功能。我可以5001像这样访问端口:locahost:5001但不能像这样:192.168.x.x:5001

我的 react-native expo 项目中有以下代码:

export const functions = firebase.functions();
firebase.functions().useFunctionsEmulator('http://192.168.x.x:5001');
Run Code Online (Sandbox Code Playgroud)

但同样,如果我将最后一行更改为:

firebase.functions().useFunctionsEmulator('http://localhost:5001');
Run Code Online (Sandbox Code Playgroud)

是否可以使用--host类似firebase serve命令的选项启动模拟器?还有其他解决方案吗?

Con*_*ner 44

将您的 firebase.json 设置为

"emulators": {
    "functions": {
      "port": 5001,
      "host": "0.0.0.0"
    },
    "firestore": {
      "port": 8080
    },
    "database": {
      "port": 9000,
      "host": "0.0.0.0"
    },
    "hosting": {
      "port": 5000
    }
  }
Run Code Online (Sandbox Code Playgroud)

“0.0.0.0”主机告诉您的计算机同时为您的网络使用 localhost 和您的本地 ip

检查 Android FirebaseDatabase 库更改日志(版本 19.1.0 解释了此新功能)https://raw.githubusercontent.com/firebase/firebase-android-sdk/6ae83de756628b933c7ddaf1153c14af0315c945/firebase-database/CHANGELOG.md

  • 就我而言,我必须将其设置为我的本地 IP 才能工作。 (2认同)
  • 正确答案 - 官方文档中对此进行了解释 - https://firebase.google.com/docs/hosting/test-preview-deploy (2认同)

eap*_*apo 5

要在不同的主机上收听,而不default: localhost只是检查--help

$ firebase serve --help
Usage: serve [options]

start a local server for your static assets

Options:

-p, --port <port>   the port on which to listen (default: 5000) (default: 5000)
-o, --host <host>   the host on which to listen (default: localhost) (default: localhost)
--only <targets>    only serve specified targets (valid targets are: functions, hosting)
--except <targets>  serve all except specified targets (valid targets are: functions, hosting)
-h, --help          output usage information
Run Code Online (Sandbox Code Playgroud)

在这种情况下:

firebase.functions().useFunctionsEmulator('http://0.0.0.0:5001');
Run Code Online (Sandbox Code Playgroud)

或者

firebase serve -o 0.0.0.0
Run Code Online (Sandbox Code Playgroud)

因为0.0.0.0表示当前网络(仅作为源地址有效)。