Home > Software design >  SignalR HubConnectionState = connecting
SignalR HubConnectionState = connecting

Time:11-03

I am trying to create SignalR server with Xamarin client. Everytime i am trying to connect to server, the status is just "connecting" without change. Tried to bypass certificate with code i found on the internet but it does not solve the problem. And when I try to go on hub site (ip:port/notifier) i get message "Connection ID required"

Client side:

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;    
        }

Server side

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! 

CodePudding user response:

So finally I found a solution!

the problem was that emulator cannot connect to localhost through http://localhost:port but with ip "10.0.2.2:port"

and for some reason implement signalR to client as service did not work aswell...

  • Related