Reputation: 16290
I have an ASP.Net Core 3.1 WebApp which includes Razor Pages, Api controllers and a SignalR hub. My Startup.cs
looks like this:
public void ConfigureServices(IServiceCollection services)
{
services.AddControllers();
services.AddRazorPages();
services.AddSignalR(options =>
{
options.EnableDetailedErrors = true;
});
services.AddMvc();
services.AddSingleton<HubManager>();
}
public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
{
app.UseDeveloperExceptionPage();
app.UseAuthentication();
app.UseHttpsRedirection();
app.UseStaticFiles();
app.UseRouting();
app.UseAuthorization();
app.UseEndpoints(endpoints =>
{
endpoints.MapRazorPages();
endpoints.MapControllers();
endpoints.MapHub<MainHub>("/MainHub");
});
}
In my razor page I add the following at the end of the script:
<script src="~/js/signalr//dist//browser/signalr.js"></script>
<script src="~/js/HubManager.js"></script>
And the HubManager.js
looks like this:
const connection = new signalR.HubConnectionBuilder()
.withUrl("/MainHub", {
skipNegotiation: true,
transport: signalR.HttpTransportType.WebSockets
})
.configureLogging(signalR.LogLevel.Information)
.withAutomaticReconnect()
.build();
async function start() {
try {
await connection.start();
console.log("connected");
} catch (err) {
console.log(err);
setTimeout(() => start(), 5000);
}
};
connection.onclose(async () => {
await start();
});
start();
I then publish the webapp and navigate to the page that uses SignalR (using Chrome). But when I check the console, I see that SignalR is polling around every 1s and this error keeps on showing:
signalr.js:4709 WebSocket connection to 'wss://myurl/MainHub' failed: Error during WebSocket handshake: Unexpected response code: 404
HubManager.js:61 Error: There was an error with the transport. at WebSocket.webSocket.onerror (signalr.js:4728)
The Websockets protocol is installed on the server. So what can the problem be?
Upvotes: 1
Views: 1909
Reputation: 27793
WebSocket connection to 'wss://myurl/MainHub' failed: Error during WebSocket handshake: Unexpected response code: 404
I can reproduce same issue, if I host the ASP.NET Core app as an IIS sub-application "MyWebApp" but not set URL with the sub-app's pathbase on SignalR JavaScript client side.
To make it work with IIS sub-application, you can try to modify the code as below then republish it to your IIS server.
const connection = new signalR.HubConnectionBuilder()
.withUrl("/MyWebApp/MainHub", {
skipNegotiation: true,
transport: signalR.HttpTransportType.WebSockets
})
.configureLogging(signalR.LogLevel.Information)
.withAutomaticReconnect()
.build();
Upvotes: 2