【问题标题】:Android Device not able to connect to scoket.io using flutterAndroid 设备无法使用颤振连接到 scoket.io
【发布时间】:2022-01-16 12:26:24
【问题描述】:

正在构建这个应用程序,我想要实时响应,所以我决定使用 socket.io 和 Nodejs。

在开发过程中,我注意到我的 iPhone 模拟器可以连接到 localhost,但是我的 android 无法连接到 localhost,所以我在一个真正的 Android 设备上进行了测试,但它无法连接。

我接下来要做的是在我的颤振 pubspec.yaml 中安装不同的 socket_io_client 版本,并在我的节点 package.json 中安装不同版本的 socket.io(服务器),但没有解决我的问题

接下来我将 Android 模拟器代理更改为 127.0.0.1 和端口 8000 和 10.0.2.2 以及端口 8000(与我的 nodejs 服务器匹配的端口)但这也不起作用

我做的最后一件事是将我的代码部署到 Heroku 服务器,我想可能无法正确配置我的模拟器以连接本地主机,但令我惊讶的是,Android 模拟器无法连接到我在 Heroku

中运行的服务器

Flutter 依赖

socket_io_client: ^2.0.0-beta.4-nullsafety.0

Nodejs (package.json)

{
  "name": "prayer_buddy_server",
  "version": "1.0.0",
  "description": "",
  "main": "index.js",
  "scripts": {
    "test": "echo \"Error: no test specified\" && exit 1",
    "start": "nodemon index.js"
  },
  "engines": {
    "node": "14.18.1"
  },
  "author": "zionnite",
  "license": "ISC",
  "dependencies": {
    "agora-access-token": "^2.0.4",
    "express": "^4.17.1",
    "nodemon": "^2.0.15",
    "socket.io": "^4.4.0",
    "uuid": "^8.3.2"
  }
}

Flutter Doctor 摘要

Flutter 2.5.3 • channel stable • https://github.com/flutter/flutter.git
Framework • revision 18116933e7 (8 weeks ago) • 2021-10-15 10:46:35 -0700
Engine • revision d3ea636dc5
Tools • Dart 2.14.4
apple@APPLEs-MacBook-Air ~ % flutter doctor
Doctor summary (to see all details, run flutter doctor -v):
[✓] Flutter (Channel stable, 2.5.3, on macOS 11.4 20F71 darwin-x64, locale
    en-US)
[!] Android toolchain - develop for Android devices (Android SDK version 31.0.0)
    ! Some Android licenses not accepted.  To resolve this, run: flutter doctor
      --android-licenses
[✓] Xcode - develop for iOS and macOS
[✓] Chrome - develop for the web
[✓] Android Studio (version 2020.3)
[✓] VS Code (version 1.61.2)
[✓] Connected device (2 available)

! Doctor found issues in 1 category

各位大侠,我该怎么做才能解决这个问题?

【问题讨论】:

    标签: android node.js flutter socket.io


    【解决方案1】:

    我能够找出问题所在。

    iPhone 模拟器通过 http://localhost:8000 连接到 localhost

    同时

    Android 模拟器通过 http://10.0.2.2:8000 连接到 localhost

    所以我只是将这段代码放在我的项目中,就像这样,一切正常

    static String server_ip =
          Platform.isIOS ? "http://localhost" : "http://10.0.2.2";
    
      @override
      void initState() {
        print('here');
    
        socket = IO.io(
           '$server_ip:8000',
            IO.OptionBuilder()
                .setTransports(['websocket'])
                .disableAutoConnect()
                .build());
        socket.connect();
        socketListener();
        super.initState();
      }
    

    【讨论】:

      猜你喜欢
      • 2021-09-07
      • 2021-04-08
      • 2020-11-09
      • 2021-02-04
      • 2021-06-21
      • 1970-01-01
      • 1970-01-01
      • 2021-06-08
      • 2021-06-26
      相关资源
      最近更新 更多