@Cold-Egg I did the following steps for my Blazor application. The page loads fine, however, it cannot establish a websocket connection to my server. I tried adding ports 80, 5000, and 5001 for "/" in my WebSocket configuration in OpenLiteSpeed, but that does not fix the issue of the web socket.
The rest of the app loads fine though, it just cannot establish a WebSocket connection. How do I fix this?
There can be many reasons why SignalR or websockets do not connect in OpenLiteSpeed. I had the same problem yesterday, but now it is working fine. But my problem was with OpenLiteSpeed specifically denying the WebSocket connection, and had nothing to do with ASP.NET code. So, do this in this case:
As recommended by Microsoft, add the following code in Program.cs:
services.AddCors();
services.AddControllers()
.AddJsonOptions(x => x.JsonSerializerOptions.DefaultIgnoreCondition = JsonIgnoreCondition.WhenWritingNull);
builder.Services.Configure<ForwardedHeadersOptions>(options =>
{
options.ForwardedHeaders =
ForwardedHeaders.XForwardedFor | ForwardedHeaders.XForwardedProto;
});
builder.Services.AddSignalR(hubOptions =>
{
hubOptions.EnableDetailedErrors = true;
hubOptions.KeepAliveInterval = TimeSpan.FromSeconds(10);
hubOptions.HandshakeTimeout = TimeSpan.FromSeconds(10);
});
builder.Services.AddAuthentication();
app.UseRouting();
app.UseHttpsRedirection();
app.MapControllers();
app.UseEndpoints(endpoints =>
{
endpoints.MapHub<SignalRHub>("/your_hub_name");
});
app.UseForwardedHeaders(new ForwardedHeadersOptions
{
ForwardedHeaders = ForwardedHeaders.XForwardedFor | ForwardedHeaders.XForwardedProto
});
app.UseAuthentication();
app.UseWebSockets();
Make sure full debugging is enabled, otherwise it will be difficult to debug:
builder.Logging.AddFilter("Microsoft.AspNetCore.SignalR", LogLevel.Debug);
builder.Logging.AddFilter("Microsoft.AspNetCore.Http.Connections", LogLevel.Debug);
appsetting.json:
"Logging": {
"LogLevel": {
"Default": "Debug",
"System": "Information",
"Microsoft": "Information",
"Microsoft.AspNetCore": "Warning",
"Microsoft.AspNetCore.SignalR": "Debug",
"Microsoft.AspNetCore.Http.Connections": "Debug"
}
Upload your latest code.
In the last:
In OpenLiteSpeed -> Virtual Hosts -> <Your domain / subdomain for ASP.NET that has SignalR> -> Web Socket Proxy -> Click + sign on right side. Add these values:
URI: /
Port: Your port: 5000 / 6000 or whichever you are using.
Note: Make sure you do not add the name of SignalR hub in URI, like do not add: /chathub/, add only /
In linux service file, make sure you have added this line:
Environment=ASPNETCORE_URLS=
http://127.0.0.1.5000
or whichever port you are using for ASP.NET project
Do Graceful restart of OpenLiteSpeed. And restart service file of Linux, like:
sudo systemctl restart signalr.service && sudo systemctl status signalr.service
Make sure your development certificate is trusted on linux machine to run https path:
dotnet dev-certs https --clean && dotnet dev-certs https --trust
Now WebSocket should be connected.
To check WebSocket in browser, you can create a html file:
<!DOCTYPE html>
<html>
<head>
<title>SignalR Test</title>
<!-- Reference the SignalR library -->
<script src="
https://cdnjs.cloudflare.com/ajax/libs/microsoft-signalr/3.1.7/signalr.min.js"></script>
<!-- Reference jQuery -->
<script src="
https://ajax.googleapis.com/ajax/libs/jquery/3.5.1/jquery.min.js"></script>
</head>
<body>
<h1>SignalR Test Page</h1>
<script type="text/javascript">
$(function () {
// Create a connection to your SignalR hub
const connection = new signalR.HubConnectionBuilder()
.withUrl("https://asp.net_project_url/signalr_hub") // replace with the path to your hub
.configureLogging(signalR.LogLevel.Information) // enable logging
.build();
// Set up client-side hub methods
connection.on("ReceiveMessage", function (message) {
console.log(message);
});
// Start the connection
connection.start().then(function () {
console.log('Connection started');
}).catch(function (error) {
console.log('Error in connection start: ' + error);
});
});
</script>
</body>
</html>
In console, it should show: Information: WebSocket connected to wss://asp.net_project_url/signalr_hub?id=XXXXXXXXXXXXXXX