1
votes

enter image description here

The following code Jquery and asp.net core webapi is does'nt working in domin,it throw signalr websockets failed, from the attachment will find the error,but is works locally is fine.

Jquery Signalr Client Code & Asp.net Core Web API :-

if (window.jQuery) {
    var _url = window.location.hostname == 'localhost' ? _local.replace('https', 'wss') : _ip_domain.replace('https', 'wss')
    var connection = new signalR.HubConnectionBuilder().withUrl(_ip_domain,
        {
            skipNegotiation: true,
            useDefaultPath: false,
            transport: signalR.HttpTransportType.WebSockets,//WebSockets
        }).configureLogging(signalR.LogLevel.Information).build();

    connection.on("ReceiveMessage", function (json) {
       console.log('msg received');
    });
    connection.on("ConnectionId", function (conid) {
        console.log('ConnectionId :' + conid);
    });
    async function start() {
        try {
            connection.start().then(function () {
                console.log("SignalR Connected.");
            });
        } catch (err) {
            console.log(err);
            setTimeout(start, 5000);
        }
    };
    connection.onclose(start);
    start();
}

Asp.Net Core Startup :-

    public void ConfigureServices(IServiceCollection services)
    {
        services.AddHsts(options =>
        {
            options.Preload = true;
            options.IncludeSubDomains = true;
            options.MaxAge = TimeSpan.FromDays(60);
        });
        services.AddHttpsRedirection(options =>
            {
                options.RedirectStatusCode = (int)HttpStatusCode.PermanentRedirect;
                options.HttpsPort = 443;
            });

       
        services.AddSignalR();
        services.AddControllers();
        services.AddRouting(c => { c.LowercaseUrls = true; });

        services.AddSwaggerGen(swagger =>
        {
            swagger.SwaggerDoc("v1", new OpenApiInfo
            {
                Title = "IOT Core API",
                Version = "v1.1",
                Description = "API to unerstand request and response schema."                    
            });
        });
        services.AddControllers();
        services.AddDistributedMemoryCache();
        services.AddSession(options =>
        {
            options.Cookie.Name = "test";
            options.IdleTimeout = TimeSpan.FromMinutes(10);
            options.Cookie.HttpOnly = true;
            options.Cookie.IsEssential = true;
        });

        services.AddSingleton<IHttpContextAccessor, HttpContextAccessor>();
        services.AddMvcCore().AddApiExplorer();
        services.AddCors(options =>
        {
            options.AddPolicy(_cors, builder =>
            {
                builder/*.WithOrigins("https://ip_address/api/", "https://localhost:44340/", https://mywebsite.com/")*/
                .AllowAnyHeader()
                .AllowAnyMethod()
                .SetIsOriginAllowed(isOriginAllowed: _ => true)
                .AllowCredentials();
            });
        });
    }
    public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
    {
        if (env.IsDevelopment())
        {
            app.UseDeveloperExceptionPage();
        }
        app.UseHttpsRedirection();
        app.UseRouting();
        app.UseSession();
        app.UseAuthorization();
        app.UseAuthentication();
        app.UseWebSockets();
        app.UseCors(_cors);
        app.UseStaticFiles();

        app.UseEndpoints(endpoints =>
        {
            endpoints.MapHub<MotorHub>("/signalr", c =>
            {
                c.Transports = HttpTransportType.WebSockets;
            });
            endpoints.MapControllers().RequireCors(_cors);
        });
        app.UseSwagger();
        app.UseSwaggerUI(c =>
        {               
            string swaggerJsonBasePath = string.IsNullOrWhiteSpace(c.RoutePrefix) ? "." : "..";
            c.SwaggerEndpoint($"{swaggerJsonBasePath}/swagger/v1/swagger.json", "Version 1.1");
            c.SwaggerEndpoint($"{swaggerJsonBasePath}/swagger/v2/swagger.json", "Version 1.2");
        });
    
1
it seems that you restrict the client to only use the WebSockets as transports, if you try to use another transport or let SignalR automatically chooses the best transport method, does it can connect to hub?Fei Han
If i use automatic choice the signalr throws negotiate error,Devops KSR
Got it,Thanks for your suggestion bro.......!Devops KSR

1 Answers

0
votes

Juery Signalr Client Side Code :-

1.If you publish in iis server,so kindly enable websocket in server manager -> add roles -> websockets protocol and install it.

2.new signalR.HubConnectionBuilder().withUrl(_domain,{skipNegotiate : true, transport : signalr.HttpTransport.WebSockets }).configureLogging(signalR.LogLevel.Information).build(); //should remove all the restrict transports, In signalr it will automatically choose the best transport protocol.

    var _ip_domain = "https://ipaddress:port/api/signalr";//ip addresss
    var _domain = "https://www.mywebsite.com/api/signalr";//web api url core 3.1, in api 
                                                         url should mention the "api/"

    var _local = "https://localhost:44393/signalr";

    console.clear();
    "use strict";
    if (window.jQuery) {
        var _url = window.location.hostname == 'localhost' ? _local : _domain
        var connection = new signalR.HubConnectionBuilder().withUrl(_domain)
            .configureLogging(signalR.LogLevel.Information).build();

        connection.on("ReceiveMessage", function (json) {
            console.log('message recieved');
        });
        connection.on("ConnectionId", function (conid) {
            console.log('ConnectionId :' + conid);
        });
        async function start() {
            try {
                connection.start().then(function () {
                    console.log("SignalR Connected.");
                });
            } catch (err) {
                console.log(err);
                setTimeout(start, 5000);
            }
        };
        connection.onclose(start);
        start();
    }

Asp.Net Core 3.1 Web API :-

API Should Map the Signalr Path and Enable Cors Origins...!

public class Startup
{
    public Startup(IConfiguration configuration)
    {
        Configuration = configuration;
    }
    public IConfiguration Configuration { get; }
    private readonly string _cors = "AllowAllPolicy";
    public void ConfigureServices(IServiceCollection services)
    {
        services.AddHsts(options =>
        {
            options.Preload = true;
            options.IncludeSubDomains = true;
            options.MaxAge = TimeSpan.FromDays(60);
        });
        services.AddHttpsRedirection(options =>
        {
            options.RedirectStatusCode = (int)HttpStatusCode.PermanentRedirect;
            options.HttpsPort = 443;
        });            
        services.AddControllers();
        services.AddRouting(c => { c.LowercaseUrls = true; });
        services.AddSwaggerGen(swagger =>
        {
            swagger.SwaggerDoc("v1", new OpenApiInfo
            {
                Title = "IOT Core API",
                Version = "v1.1",
                Description = "API to unerstand request and response schema."
            });
        });
        services.AddControllers();
        services.AddDistributedMemoryCache();
        services.AddSession(options =>
        {
            options.Cookie.Name = ".session";
            options.IdleTimeout = TimeSpan.FromMinutes(10);
            options.Cookie.HttpOnly = true;
            options.Cookie.IsEssential = true;
        });

        services.AddSingleton<IHttpContextAccessor, HttpContextAccessor>();
        services.AddMvcCore().AddApiExplorer();
        services.AddCors(options =>
        {
            options.AddPolicy(_cors, builder =>
            {
                builder.AllowAnyHeader()
                .AllowAnyMethod()
                .SetIsOriginAllowed(isOriginAllowed: _ => true)
                .AllowCredentials();
            });
        });
        services.AddSignalR();

    }
    public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
    {
        if (env.IsDevelopment())
        {
            app.UseDeveloperExceptionPage();
        }
        else
        {
            app.UseHsts();
        }
        app.UseHttpsRedirection();
        app.UseRouting();
        app.UseSession();
        app.UseAuthorization();
        app.UseAuthentication();
        app.UseWebSockets();
        app.UseCors(_cors);//enable cors
        app.UseStaticFiles();

        app.UseEndpoints(endpoints =>
        {
            endpoints.MapHub<SignalrHub>("/signalr");//signalr path
            endpoints.MapControllers();
        });
        app.UseSwagger();
        app.UseSwaggerUI(c =>
        {
            c.DocumentTitle = "API";
            string swaggerJsonBasePath = string.IsNullOrWhiteSpace(c.RoutePrefix) ? "." : "..";
            c.SwaggerEndpoint($"{swaggerJsonBasePath}/swagger/v1/swagger.json", "Version 1.1");
            c.SwaggerEndpoint($"{swaggerJsonBasePath}/swagger/v2/swagger.json", "Version 1.2");
        });
    }

Signalr Hub Class

public class SignalrHub : Hub
{
    public override Task OnConnectedAsync()
    {
        Console.WriteLine("--> Connection Opened: " + Context.ConnectionId);
        Clients.Client(Context.ConnectionId).SendAsync("ConnectionId", Context.ConnectionId);
        return base.OnConnectedAsync();
    }
}

In your contoller class ex:valuescontroller

Should Initialize the hub context ;
private readonly IHubContext<MotorHub> hubContext;
 public ApiController(IHubContext<MotorHub> hubContext)
    {          
        this.hubContext = hubContext;            
    }
 Inside the Method you send the response to client, Paste the following lines : 
 await hubContext.Clients.All.SendAsync("ReceiveMessage", your_result);//if the result is json or any format...!