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