首页 > 解决方案 > SignalR HubConnectionState = 正在连接

问题描述

我正在尝试使用 Xamarin 客户端创建 SignalR 服务器。每次我尝试连接到服务器时,状态只是“正在连接”而没有改变。试图用我在互联网上找到的代码绕过证书,但它并没有解决问题。当我尝试访问集线器站点(ip:port/notifier)时,我收到消息“需要连接 ID”

客户端:

public class SignalRService : ISignalRService
    {
        private readonly HubConnection hubConnection;

        public SignalRService()
        {
            hubConnection = new HubConnectionBuilder()
                .WithUrl("https://ip:5001/notifier", options => {
                    options.HttpMessageHandlerFactory = factory => new HttpClientHandler
                    {
                        ServerCertificateCustomValidationCallback = (message, cert, chain, errors) => { return true; }
                    };
                })
                .WithAutomaticReconnect()
                .Build();
        }

        public void /*async Task*/ Connect()
        {
            hubConnection.StartAsync();
            var a = hubConnection.State;
            while (hubConnection.State == HubConnectionState.Connecting) //for debug purposes
            {
                continue;
            }
            a = hubConnection.State;    
        }

服务器端

public void ConfigureServices(IServiceCollection services)
        {
            services.AddRazorPages();
            services.AddSignalR();
        }

        public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
        {
            if (env.IsDevelopment())
            {
                app.UseDeveloperExceptionPage();
            }
            else
            {
                app.UseExceptionHandler("/Error");
                app.UseHsts();
            }

            app.UseHttpsRedirection();
            app.UseStaticFiles();

            app.UseRouting();

            app.UseAuthorization();

            app.UseEndpoints(endpoints =>
            {
                endpoints.MapRazorPages();
                endpoints.MapHub<NotifyHub>("/notifier");
            });
        }

Thanks for any help! 

标签: c#xamarinserversignalrsignalr-hub

解决方案


所以最后我找到了解决方案!

问题是模拟器无法通过 http://localhost:port 连接到 localhost 但使用 ip "10.0.2.2:port"

并且由于某种原因向客户端实施 signalR,因为服务也无法正常工作......


推荐阅读