1

您好,我最近在使用 SignalR 和 Xamarin 时遇到了一些问题。我使用了一个 Asp.net 核心 Web 应用程序和一个使用 SignalR 客户端包的 xamarin 表单。每当我尝试连接时,我都会收到错误“连接被拒绝”,我已经将我的代码与其他做同样事情的教程进行了比较,我相信它们匹配但我仍然遇到问题。这是我的代码:

Xamarin 客户端代码

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Microsoft.AspNetCore.SignalR.Client;
using Xamarin.Forms;
using Xamarin.Forms.Xaml;
namespace Neighbor_Mobile.SubPages
{
[XamlCompilation(XamlCompilationOptions.Compile)]
public partial class GroupPage : ContentPage
{
    HubConnection connection;
    public GroupPage()
    {
        InitializeComponent();

        AttemptConnect();
    }

    private async void AttemptConnect()
    {
        try
        {
            connection = new HubConnectionBuilder()
                .WithUrl("https://localhost:44353/chat")
                .Build();
            connection.On<string>("ReceiveMessage", (message) => Display.Text = message);
            await SignalRConnect();
        }
        catch (Exception e)
        {
            error.Text = $"error: {e}"; 
        }
    }

    private async Task SignalRConnect()
    {
        try
        {
            //the error occurs here and prints "Connection Refused" to a debug label i'm using
            await connection.StartAsync();
        }
        catch (Exception ex)
        {
            Display.Text = ex.Message;
            return;
        }
    }

    }
}

这是我在服务器上的启动页面代码:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using FleetSocketServer.Hubs;
using Microsoft.AspNetCore.Builder;
using Microsoft.AspNetCore.Hosting;
using Microsoft.AspNetCore.Http;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Hosting;

namespace FleetSocketServer
{
public class Startup
{
    public void ConfigureServices(IServiceCollection services)
    {
        services.AddSignalR();
    }

    public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
    {
        if (env.IsDevelopment())
        {
            app.UseDeveloperExceptionPage();
        }

        app.UseRouting();

        app.UseEndpoints(endpoints =>
        {
            endpoints.MapHub<SocketGroupHub>("/chat");
        });
    }
}
}

我的中心页面代码:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Microsoft.AspNetCore.SignalR;

namespace FleetSocketServer.Hubs
{
public class SocketGroupHub :Hub
{
    public async Task SendMessage(string message)
    {
        await Clients.All.SendAsync("Recieve", message);
    }
}
} 

我的 launchSettings 我尝试使用 iisSettigns 和 FleetSocketServer 调试它,同时更改 WithURL 中的 URl,但仍然给我同样的错误

{
"iisSettings": {
"windowsAuthentication": false, 
"anonymousAuthentication": true, 
"iisExpress": {
  "applicationUrl": "http://localhost:35579",
  "sslPort": 44353
 }
 },
 "profiles": {
 "IIS Express": {
  "commandName": "IISExpress",
  "launchBrowser": true,
  "environmentVariables": {
    "ASPNETCORE_ENVIRONMENT": "Development"
  }
  },
  "FleetSocketServer": {
  "commandName": "Project",
  "launchBrowser": false,
  "applicationUrl": "https://localhost:5001;http://localhost:5050",
  "environmentVariables": {
    "ASPNETCORE_ENVIRONMENT": "Development"
  }
 }
}
}

谢谢!

4

1 回答 1

0

好吧,我真的很愚蠢,原因是由于明显的原因,andriod 模拟器使用 10.0.2.2 作为 localhost 而不是 127.0.0.1。

于 2021-08-30T07:03:27.570 回答