Flutter AspNet Core SocketException (SocketException: OS Error: Connection timed out, errno = 110, address = 192.168.1.87, port = 46186)

Flutter AspNet Core SocketException (SocketException: OS Error: Connection timed out, errno = 110, address = 192.168.1.87, port = 46186)

我正在尝试使用我的智能手机(不是模拟器)连接到我的带有 SignalR 后端的 Aspnet Core。 我将 Cors 添加到 Startup.cs:

public void ConfigureServices(IServiceCollection services)
    {
        services.AddCors(options =>
        {
            options.AddPolicy(MyAllowSpecificOrigins,
                builder => builder.SetIsOriginAllowed((host) => true)
                .AllowAnyMethod()
                .AllowAnyHeader()
                .AllowCredentials());
        });

        services.AddSignalR();
    }

readonly string MyAllowSpecificOrigins = "AllowOrigins";
...

app.UseCors(MyAllowSpecificOrigins);

我尝试连接 Angular 前端,它运行良好,我想对我的 Flutter 应用程序进行同样的操作。 这是代码的一部分:

const url = 'http://192.168.1.87:44327/signalrtc';

class _MyHomePageState extends State<MyHomePage> {
  final hubConnection = HubConnectionBuilder().withUrl("$url").build();

  @override
  void initState() {
    super.initState();

    hubConnection.onclose((_) {
    print("Connessione persa");
    });

    hubConnection.on("ReceiveMessage", onReceiveMessage);
    startConnection();
  }

  void startConnection() async {
    print('start');
    await hubConnection.start();
  }

我只使用 Flutter SignalR package 并且我复制了我的本地计算机 IPv4 以连接到我的后端,因为正如我已经说过的,我没有使用模拟器,所以我无法插入 10.0.2.2,但是如果我尝试 运行 应用程序,它会给我这个错误。 我的电脑和智能手机都连接到同一个网络。

有什么想法吗?

已解决

我刚刚为 Visual Studio 安装了 Conveyor by Keyoti

With it you don't have to change any configuration, it just runs in the background and gives you a port you can use remotely, and also it can tunnel to the internet so you have a public URL if you like.

您只需安装它,运行 在调试模式下打开 Keyoti,您会发现另一个 URL 可用于您的智能手机。

最后我把我在Flutter项目中的URL改成了:

const url = 'http://192.168.1.87:44327/signalrtc';

收件人:

const url = 'https://192.168.1.87:45456/signalrtc';

基本上我的 IPv4 是从 keyote 给出的,但是有另一个端口。