Reputation: 1512
I have created a Blazor WebAssembly Solution according to This Tutorial. The index page (code below, unchanged from tutorial):
What is causing this? How can I debug the client side?
@page "/"
@using Microsoft.AspNetCore.SignalR.Client
@inject NavigationManager NavigationManager
<div class="form-group">
<label>
User:
<input @bind="_userInput" />
</label>
</div>
<div class="form-group">
<label>
Message:
<input @bind="_messageInput" size="50" />
</label>
</div>
<button @onclick="Send" disabled="@(!IsConnected)">Send</button>
<hr>
<ul id="messagesList">
@foreach (var message in _messages)
{
<li>@message</li>
}
</ul>
@code {
private HubConnection _hubConnection;
private List<string> _messages = new List<string>();
private string _userInput;
private string _messageInput;
protected override async Task OnInitializedAsync()
{
_hubConnection = new HubConnectionBuilder()
.WithUrl(NavigationManager.ToAbsoluteUri("/chatHub"))
.Build();
_hubConnection.On<string, string>("ReceiveMessage", (user, message) =>
{
var encodedMsg = $"{user}: {message}";
_messages.Add(encodedMsg);
StateHasChanged();
});
await _hubConnection.StartAsync();
}
Task Send() =>
_hubConnection.SendAsync("SendMessage", _userInput, _messageInput);
public bool IsConnected =>
_hubConnection.State == HubConnectionState.Connected;
}
Below is a console app bare minimum that connects to the public signalR hub without any problems.
using System;
using Microsoft.AspNetCore.SignalR.Client;
namespace ConsoleSignalRDebug
{
class Program
{
static private HubConnection _hubConnection;
static void Main(string[] args)
{
try
{
_hubConnection = new HubConnectionBuilder()
.WithUrl("https://myblazorSite.com:443/chatHub")
.Build();
_hubConnection.On<string, string>("ReceiveMessage", (user, message) =>
{ Console.WriteLine("/////////////RECEIVED///////////////" + user + " " + message);});
_hubConnection.StartAsync().GetAwaiter().GetResult();
string x = Console.ReadLine();
_hubConnection.SendAsync("SendMessage", x, x);
Console.ReadLine();
}
catch (Exception e)
{
throw;
}
}
}
}
I tried this for debugging but it only works on the console (that works anyways):
_hubConnection = new HubConnectionBuilder()
.WithUrl("https://myblazorSite.com:443/chatHub")
.ConfigureLogging(logging =>
{
logging.SetMinimumLevel(LogLevel.Debug);
logging.AddConsole();
})
.Build();
Upvotes: 0
Views: 2709
Reputation: 13468
WebSockets is enabled by default on IISExpress (used for local testing) but disabled by default on full-fat IIS.
So when deploying an application using WebSockets, such as SignalR (or server-side Blazor) you need to enable WebSockets on IIS.
The instructions for enabling IIS WebSockets are found at https://learn.microsoft.com/en-us/aspnet/core/fundamentals/websockets?view=aspnetcore-3.1#iisiis-express-support
Upvotes: 2